ADVANCE CODING
JAGADEESHWAR REDDY BUSIREDDY
BU21EECE0100177
Assignment -1
1. Basic Arithmetic
Write a C program to calculate the area of a circle given its radius. Use the formula Area=πr2, π as a
constant.
#include <stdio.h> #define PI
3.14159
int main() {
float radius, area;
printf("Enter the radius of the circle:”);
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area); return
0;
//BU21EECE0100177
2. Swapping Numbers
Write a program to swap two numbers without using a third variable.
#include <stdio.h>
int main()
{ int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
//BU21EECE0100177
3. Leap Year
Write a program to check whether a given year is a leap year or not. A year is a leap year if it is
divisible by 4 but not divisible by 100, unless it is also divisible by 400.
#include <stdio.h>
int main()
{ int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{ printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
return 0;
//BU21EECE0100153
4. Maximum of Three Numbers
Write a program to find the largest of three numbers using nested if-else statements.
#include <stdio.h>
int main()
{ int a, b,
c;
printf("Enter three numbers:
"); scanf("%d %d %d", &a, &b,
&c); if (a > b) {
if (a > c) {
printf("The largest number is: %d\n", a);
} else {
printf("The largest number is: %d\n", c);
} else {
if (b > c) {
printf("The largest number is: %d\n", b);
} else {
printf("The largest number is: %d\n", c);
return 0;
5. Multiplication Table
Write a program to print the multiplication table of a given number up to 10 using a for
loop. #include <stdio.h>
int main()
{ int n;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
6. Number Reversal
Write a program to reverse a given number. For example, if the input is 123, the output should be
321.
#include <stdio.h>
int main() {
int num, reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
printf("Reversed number: %d\n", reverse);
return 0;
}
7. Palindrome Number
Write a program to check if a given number is a palindrome. A number is a palindrome if it reads the
same forward and backward (e.g., 121, 12321).
#include <stdio.h>
int main() {
int num, original, reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
if (original == reverse) {
printf("%d is a palindrome.\n", original);
} else {
printf("%d is not a palindrome.\n", original);
return 0;
}
8. Prime Number
Write a program to check whether a given number is a prime number. A prime number is only
divisible by 1 and itself.
#include <stdio.h>
int main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
{ isPrime = 0;
} else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
return 0;
9. Sum of Digits
Write a program to calculate the sum of digits of a given number. For example, if the input is 123,
the output should be 1+2+3=61 + 2 + 3 = 61+2+3=6.
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
sum += num % 10;
num /= 10;
printf("Sum of digits: %d\n", sum);
return 0;
10. Factorial
Write a program to calculate the factorial of a number using a while loop. For example, 5!
=5×4×3×2×1=120
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
int i = 1;
while (i <= num)
{ factorial *= i;
i++;
}
printf("Factorial of %d is: %d\n", num, factorial);
return 0;