150 Categorized C Programs with Answers
Program 1: Hello, World!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Program 2: Print name, age, and address
#include <stdio.h>
int main() {
printf("Name: John Doe\nAge: 20\nAddress: 123 Main Street\n");
return 0;
}
Program 3: Add two integers
#include <stdio.h>
int main() {
int a = 5, b = 10, sum;
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
Program 4: Multiply two floating-point numbers
#include <stdio.h>
int main() {
float a = 2.5, b = 4.2;
float product = a * b;
printf("Product: %.2f\n", product);
return 0;
}
Program 5: Size of int, float, double, char
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu bytes\n", sizeof(char));
return 0;
}
Program 6: Swap using third variable
#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
150 Categorized C Programs with Answers
temp = a;
a = b;
b = temp;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Program 7: Swap without third variable
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Program 8: Simple Interest
#include <stdio.h>
int main() {
float p = 1000, r = 5, t = 2, si;
si = (p * r * t) / 100;
printf("Simple Interest = %.2f\n", si);
return 0;
}
Program 9: Area of circle
#include <stdio.h>
#define PI 3.1416
int main() {
float r = 7, area;
area = PI * r * r;
printf("Area = %.2f\n", area);
return 0;
}
Program 10: Area of rectangle
#include <stdio.h>
int main() {
float l = 5, b = 3;
float area = l * b;
printf("Area of rectangle = %.2f\n", area);
return 0;
}