0% found this document useful (0 votes)
6 views

List of Program with solution

The document contains a list of 25 programming experiments in C, each detailing a specific task such as displaying 'Hello World', performing arithmetic operations, and implementing various algorithms. Each task is accompanied by a code snippet demonstrating how to achieve the desired functionality. The experiments cover a range of topics including control structures, functions, and basic data handling.

Uploaded by

RAJAT RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

List of Program with solution

The document contains a list of 25 programming experiments in C, each detailing a specific task such as displaying 'Hello World', performing arithmetic operations, and implementing various algorithms. Each task is accompanied by a code snippet demonstrating how to achieve the desired functionality. The experiments cover a range of topics including control structures, functions, and basic data handling.

Uploaded by

RAJAT RAJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

List of Experiments

1. Write a program in c to display hello world.

2. WAP in c to find addition of two number.

3. WAP in c to find quotient and remainder of the given no.

4. WAP in c to find area of circle.

5. WAP in c to find rectangle of the given values.

6. WAP in c to find simple interest.

7. WAP in c to find power of the given number.

8. WAP in c to check weather given no is positive or negative.

9. WAP in c to check weather given no is even or odd.

10. WAP in c for swapping of two number.

11. WAP in c for swapping of two number without using third variable.

12. WAP in c to find largest no between two numbers.

13. WAP in c to find largest no between three numbers.

14. WAP in c to design calculator with such operation (Addition,Sub,Multiply,division)

15. WAP in c to display month name by using month no.

16. WAP in c to display table of given no.

17. WAP in c to find factorial of the given no.

18. WAP in c to find sum of n natural number.

19. WAP in c to check given year is leap year or not.

20. WAP in c to find reverse of the given number.

21. WAP in c to check given no is palindrome or not.

22. WAP in c to find sum of all digits in given number.

23. WAP in c design calculator using switch statement.

24. WAP in c display month name by month no using switch statement.

25. WAP in c to find factorial of the given number using recursion.


1. Write a program in C to display "Hello World".

#include <stdio.h>
int main() {
printf("Hello World\n");
return 0;
}
2. WAP in C to find addition of two numbers.

#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("The sum is: %d\n", sum);
return 0;
}
3. WAP in C to find quotient and remainder of the given number.

#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend and divisor: ");
scanf("%d %d", &dividend, &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient: %d, Remainder: %d\n", quotient, remainder);
return 0;
}

4. WAP in C to find area of circle.

#include <stdio.h>
#define PI 3.14
int main() {
float radius, area;
printf("Enter radius of circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of circle: %f\n", area);
return 0;
}
5. WAP in C to find rectangle of the given values.

#include <stdio.h>
int main() {
int length, width, area;
printf("Enter length and width of rectangle: ");
scanf("%d %d", &length, &width);
area = length * width;
printf("Area of rectangle: %d\n", area);
return 0;
}

6. WAP in C to find simple interest.

#include <stdio.h>
int main() {
float principal, rate, time, interest;
printf("Enter principal, rate, and time: ");
scanf("%f %f %f", &principal, &rate, &time);
interest = (principal * rate * time) / 100;
printf("Simple interest: %f\n", interest);
return 0;
}
7. WAP in C to find power of the given number.

#include <stdio.h>
#include <math.h>
int main() {
int base, exponent, result;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exponent);
result = pow(base, exponent);
printf("Power: %d\n", result);
return 0;
}
8. WAP in C to check whether given number is positive or negative.

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("Number is positive\n");
} else if (num < 0) {
printf("Number is negative\n");
} else {
printf("Number is zero\n");
}
return 0;}
9. WAP in C to check whether given number is even or odd.

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Number is even\n");
} else {
printf("Number is odd\n");
}
return 0;
}
10. WAP in C for swapping of two numbers.

#include <stdio.h>
int main() {
int num1, num2, temp;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("Swapped numbers: %d %d\n", num1, num2);
return 0;
}
11. WAP in C for swapping of two numbers without using third variable.

#include <stdio.h>

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("Swapped numbers: %d %d\n", num1, num2);
return 0;
}
12. WAP in C to find largest number between two numbers.

#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("Largest number: %d\n", num1);
} else {
printf("Largest number: %d\n", num2);
}
return 0;
}
13. WAP in C to find largest number between three numbers.

#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 && num1 > num3) {
printf("Largest number: %d\n", num1);
} else if (num2 > num1 && num2 > num3) {
printf("Largest number: %d\n", num2);
} else {
printf("Largest number: %d\n", num3);
}
return 0;
}
14. WAP in C to design calculator with such operation (Addition, Sub, Multiply, division)

#include <stdio.h>
int main() {
int choice;
float num1, num2, result;
printf("Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if {
result = num1 + num2;
}
else if
{

result = num1 - num2;


break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Invalid operation”); }
}
}

15. WAP in C to display month name by using month no.


#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number\n");
break;
}
return 0;
}
16. WAP in C to display table of given no.

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Table of %d:\n", num);
for (int i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", num, i, num * i);
}
return 0;
}

17. WAP in C to find factorial of the given no.


#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d: %d\n", num, factorial);
return 0;
}

18. WAP in C to find sum of n natural number.


#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
sum += i;
}
printf("Sum of first %d natural numbers: %d\n", num, sum);
return 0;
}
19. WAP in C to check given year is leap year or not.

#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;
}

20. WAP in C to find reverse of the given number.


#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("Reverse of the given number: %d\n", reverse);
return 0;
}

21. WAP in C to check given no is palindrome or not.


#include <stdio.h>
int main() {
int num, reverse = 0, original;
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;}
22. WAP in C to find sum of all digits in given number.

#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;
}
23. WAP in C design calculator using switch statement.
#include <stdio.h>

int main() {
int choice;
float num1, num2, result;
printf("Calculator Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero\n");
return 0;
}
break;
default:
printf("Invalid choice\n");
return 0;
}
printf("Result: %f\n", result);
return 0;
}
24. WAP in C display month name by month no using switch statement.
#include <stdio.h>

int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number\n");
break;
}
}

25. WAP in C to find the factorial of a given number using recursion:

#include <stdio.h>

long long factorial(int n) {


if (n == 0 || n == 1) {

return 1;
} else {

return n * factorial(n - 1);


}
}

int main() {

int num;

printf("Enter a positive integer: ");


scanf("%d", &num);

if (num < 0) {

printf("Error: Factorial is not defined for negative numbers.\n");


} else {
printf("Factorial of %d is %lld\n", num, factorial(num));

return 0;}

You might also like