Program
1. Area of triangle
2. Area of rectangle
3. Area of circle
4. Volume of sphere
5. Swap two numbers
6. Even and odd
7. Leap year
8. Vowel
9. Greatest number
10. Factorial *
11. Table of inputted number
12. Ascii code
13. Series (2 4 6 8...)
1
Q1. Area of triangle
#include <stdio.h>
int main() {
float base, height, area;
printf("Enter base of the triangle: ");
scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);
area = (base * height) / 2;
printf("Area of the triangle = %.2f\n", area);
return 0;
}
Q2: Area of Rectangle (A = w × h)
#include <stdio.h>
int main() {
int w, h, area;
printf("Enter width and height: ");
scanf("%d %d", &w, &h);
area = w * h;
printf("Area = %d\n", area);
return 0;
}
Q3. Area of circle
#include <stdio.h>
int main() {
float r, area;
printf("Enter radius: ");
scanf("%f", &r);
area = 3.14 * r * r;
printf("Area of Circle = %.2f", area);
return 0;
}
2
4. Volume of sphere
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, volume;
printf("Enter the radius of the sphere: ");
scanf("%f", &radius);
volume = (4.0 / 3.0) * PI * radius * radius * radius;
printf("Volume of the sphere = %.2f\n", volume);
return 0;
}
5. Swap two numbers
#include <stdio.h>
int main() {
int a, b, temp;
// Input two numbers
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
// Swapping using a temporary variable
temp = a;
a = b;
b = temp;
// Output the swapped numbers
printf("After swapping:\n");
printf("First number = %d\n", a);
printf("Second number = %d\n", b);
return 0;
}
6. Even and odd
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n % 2 == 0)
printf("Even Number\n");
else
printf("Odd Number\n");
return 0;
}
3
7. Leap year
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if (year % 4 == 0)
printf("Leap year");
else
printf("Not a leap year");
return 0;
}
8. Vowel
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
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("Vowel");
else
printf("Not a vowel");
return 0;
}
Explanation :
This if condition checks if the entered character is any of the vowels.
✔ It checks lowercase vowels (a e i o u) and uppercase vowels (A E I O U).
✔ || means OR, so if ANY one of these conditions is true, it becomes a vowel.
9. Greatest number
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if(a > b)
printf("Greater number = %d\n", a);
else
printf("Greater number = %d\n", b);
return 0;
}
4
10. Factorial *
#include <stdio.h>
int main() {
int n, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
fact = fact * i;
}
printf("Factorial = %d\n", fact);
return 0;
}
11. Table of inputted number
#include <stdio.h>
int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
12. Ascii code
#include <stdio.h>
int main() {
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
printf("ASCII value of %c is %d", ch, ch);
return 0;
}
5
13. Series (2 4 6 8...)
#include <stdio.h>
int main() {
int n, i;
printf("Enter how many terms you want: ");
scanf("%d", &n);
printf("Series: ");
for(i = 1; i <= n; i++) {
printf("%d ", 2 * i); // 2, 4, 6, 8...
}
return 0;
}
Explanation:
We declare two integer variables:
o n → the number of terms in the series (how many numbers to print)
o i → a counter used in the loop
printf("Enter how many terms you want: ");
This prints a message on the screen asking the user to enter a number.
scanf("%d", &n);
This reads the number entered by the user and stores it in variable n.
%d is used because n is an integer.
&n gives the address of n so scanf can store the input there.
printf("Series: ");
This prints the word Series: before showing the numbers.
for(i = 1; i <= n; i++) {
o printf("%d ", 2 * i); // 2, 4, 6, 8...
}
for loop runs from i = 1 to i = n.
Each time the loop runs:
o 2 * i calculates the even number in the series.
o printf("%d ", 2 * i); prints the number followed by a space.
Example:
o i = 1 → 2×1 = 2
o i = 2 → 2×2 = 4
o i = 3 → 2×3 = 6
o and so on…