1. Home
  2. Docs
  3. C Projects
  4. Library Management System
  5. C Programs

C Programs

1.) Write a program in C language to display sum of 8 and 9.

#include <stdio.h>

int main() {
int num1 = 8;
int num2 = 9;
int sum = num1 + num2;

printf("The sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

2.) Write a program in C language to display sum, difference and product of 9 and 12.

#include <stdio.h>

int main() {
int num1 = 9;
int num2 = 12;
int sum = num1 + num2;
int difference = num2 - num1;
int product = num1 * num2;

printf("Sum of %d and %d is %d\n", num1, num2, sum);
printf("Difference of %d and %d is %d\n", num2, num1, difference);
printf("Product of %d and %d is %d\n", num1, num2, product);

return 0;
}

3.) Write a program in C language to display average of 5, 9 and 12.

#include <stdio.h>

int main() {
int num1 = 5;
int num2 = 9;
int num3 = 12;
float average = (num1 + num2 + num3) / 3.0;

printf("Average of %d, %d, and %d is %.2f\n", num1, num2, num3, average);

return 0;
}

4.) Write a program in C language to add and display any two numbers input by user.

#include <stdio.h>

int main() {
int num1, num2, sum;

printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");
scanf("%d", &num2);

sum = num1 + num2;

printf("Sum of %d and %d is %d\n", num1, num2, sum);

return 0;
}

5.) Write a program in C language to display average of any three numbers supplier by a user.

#include <stdio.h>

int main() {
int num1, num2, num3;
float average;

printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");
scanf("%d", &num2);

printf("Enter third number: ");
scanf("%d", &num3);

average = (num1 + num2 + num3) / 3.0;

printf("Average of %d, %d, and %d is %.2f\n", num1, num2, num3, average);

return 0;
}

6.) Write a program in C language to accept length and breadth of a rectangle and display area of rectangle.

#include <stdio.h>

int main() {
float length, breadth, area;

printf("Enter length of the rectangle: ");
scanf("%f", &length);

printf("Enter breadth of the rectangle: ");
scanf("%f", &breadth);

area = length * breadth;

printf("Area of the rectangle is %f\n", area);

return 0;
}

7.) Write a program in C language to accept length, breadth and height of a room and display area of four walls of the room.

#include <stdio.h>

int main() {
float length, breadth, height, area;

printf("Enter length of the room: ");
scanf("%f", &length);

printf("Enter breadth of the room: ");
scanf("%f", &breadth);

printf("Enter height of the room: ");
scanf("%f", &height);

area = 2 * height * (length + breadth);

printf("Area of the four walls of the room is %.2f\n", area);

return 0;
}

8.) Write a program in C language to accept temperature in celsius and display the equivalent temperature in fahrenheit.

#include <stdio.h>

int main() {
float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);

fahrenheit = (celsius * 9/5) + 32;

printf("Equivalent temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;
}

9.) Write a program in C language to accept radius of a circle and display area of circle.

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

10.) Write a program in C language to accept radius and height of a cylinder and display the volume of the cylinder.

#include <stdio.h>
#define PI 3.14159

int main() {
float radius, height, volume;

printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);

printf("Enter the height of the cylinder: ");
scanf("%f", &height);

volume = PI * radius * radius * height;

printf("Volume of the cylinder: %.2f\n", volume);

return 0;
}

11.) Write a program in C language to check whether a number is odd or even.

#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");
scanf("%d", &number);

if (number % 2 == 0)
printf("%d is even.\n", number);
else
printf("%d is odd.\n", number);

return 0;
}

12.) Write a program in C language to check whether a number is completely divisible by 7 or not.

#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");
scanf("%d", &number);

if (number % 7 == 0)
printf("%d is completely divisible by 7.\n", number);
else
printf("%d is not completely divisible by 7.\n", number);

return 0;
}

13.) Write a program in C language to check whether a number is positive, negative or zero.

#include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");
scanf("%d", &number);

if (number > 0)
printf("%d is positive.\n", number);
else if (number < 0)
printf("%d is negative.\n", number);
else
printf("%d is zero.\n", number);

return 0;
}

14.) Write a program in C language to accept cost price and selling price of a good and calculate and display loss or profit amount.

#include <stdio.h>

int main() {
float costPrice, sellingPrice, profit, loss;

printf("Enter the cost price: ");
scanf("%f", &costPrice);

printf("Enter the selling price: ");
scanf("%f", &sellingPrice);

if (sellingPrice > costPrice) {
profit = sellingPrice - costPrice;
printf("Profit amount: %f\n", profit);
} else if (sellingPrice < costPrice) {
loss = costPrice - sellingPrice;
printf("Loss amount: %f\n", loss);
} else {
printf("No profit, no loss.\n");
}

return 0;
}

15.) Write a program in C language to display the greatest number among any three numbers supplier by a user.

#include <stdio.h>

int main() {
int num1, num2, num3;

printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");
scanf("%d", &num2);

printf("Enter the third number: ");
scanf("%d", &num3);

if (num1 >= num2 && num1 >= num3)
printf("The greatest number is: %d\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("The greatest number is: %d\n", num2);
else
printf("The greatest number is: %d\n", num3);

return 0;
}

16.) Write a program in C language to display numbers from 1 to 15.

#include <stdio.h>

int main() {
for (int i = 1; i <= 15; i++) {
printf("%d\n", i);
}

return 0;
}

17.) Write a program in C language to display numbers from 20 to 10.

#include <stdio.h>

int main() {
for (int i = 20; i >= 10; i--) {
printf("%d\n", i);
}

return 0;
}

18.) Write a program in C language to display “Welcome to world of C language” ten times.

#include <stdio.h>

int main() {
for(int i = 0; i < 10; i++) {
printf("Welcome to world of C language\n");
}
return 0;
}

19.) Write a program in C language to display the square numbers from 2 to 10.

#include <stdio.h>

int main() {
int i;

printf("Square numbers from 2 to 10:\n");
for(i = 2; i <= 10; i++) {
printf("%d ", i * i);
}
printf("\n");

return 0;
}

20.) Write a program in C language to display the even numbers from 2 to 30.

#include <stdio.h>

int main() {
for(int i = 2; i <= 30; i += 2) {
printf("%d\n", i);
}
return 0;
}

21.) Write a program in C language to display the odd numbers from 5 to 35.

#include <stdio.h>

int main() {
for(int i = 5; i <= 35; i += 2) {
printf("%d\n", i);
}
return 0;
}

22.) Write a program in C language to count and display total number of factors of a number supplier by user.

#include <stdio.h>

int main() {
int num, count = 0;

printf("Enter a number: ");
scanf("%d", &num);

for(int i = 1; i <= num; i++) {
if(num % i == 0) {
count++;
}
}

printf("Total number of factors of %d is %d\n", num, count);

return 0;
}

23.) Write a program in C language to display list of factors of a number supplier by user.

#include <stdio.h>

int main() {
int num;

printf("Enter a number: ");
scanf("%d", &num);

printf("Factors of %d are: ", num);
for(int i = 1; i <= num; i++) {
if(num % i == 0) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

24.) Write a program in C language to check and display whether a number is prime or composite.

#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 a composite number.\n", num);
}

return 0;
}

25.) Write a program in C language to display reverse of a supplier 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 = num/10;
}

printf("Reversed number is %d\n", reverse);

return 0;
}

26.) Write a program in C language to check and display whether an alphabet is vowel or consonant.

#include <stdio.h>

int main() {
char ch;

printf("Enter an alphabet: ");
scanf(" %c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}

return 0;

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *