Least Important Programs
1. Write a program that takes radius of a circle as input. The program should calcualte and display the area of a circle.
- #include<stdio.h>
- int main() {
- float radius, area;
- // Formula: Area = π × r × r
- printf("Enter radius: ");
- scanf("%f", &radius);
- area = 3.1416 * radius * radius;
- printf("Area of circle = %f", area);
- return 0;
- }
Enter radius: 5
Area of circle = 78.540000
2. Write a program that takes the age of a person as input and display teenager if the age lies between 13 and 19.
- #include<stdio.h>
- int main() {
- int age;
- // If age is between 13 and 19, person is a teenager
- printf("Enter age: ");
- scanf("%d", &age);
- if(age >= 13 && age <= 19)
- printf("Teenager");
- else
- printf("Not a Teenager");
- return 0;
- }
Enter age: 15
Teenager
Output (Else case):
Enter age: 25
Not a Teenager
3. Write a program that will input 2 numbers from user and check first one is a factor of second one or not.
- #include<stdio.h>
- int main() {
- int num1, num2;
- // A number is a factor of another if second number % first number == 0
- printf("Enter two numbers: ");
- scanf("%d %d", &num1, &num2);
- if(num2 % num1 == 0)
- printf("%d is a factor of %d", num1, num2);
- else
- printf("%d is not a factor of %d", num1, num2);
- return 0;
- }
Enter two numbers: 5 20
5 is a factor of 20
Enter two numbers: 3 20
3 is not a factor of 20
4. Write a program that stores the age of 5 persons in an array and then display on screen
- #include<stdio.h>
- int main() {
- // Hard-coded ages of 5 persons
- int ages[5] = {18, 21, 16, 25, 19};
- // Display ages without using loop
- printf("Ages of persons are: %d %d %d %d %d",
- ages[0], ages[1], ages[2], ages[3], ages[4]);
- return 0;
- }
Ages of persons are: 18 21 16 25 19
Most Important Programs
1. Write a program that will input a number from user and check whether it is even or odd.
- #include<stdio.h>
- int main() {
- int num;
- // If a number is divisible by 2, it is Even; otherwise Odd
- printf("Enter a number: ");
- scanf("%d", &num);
- if(num % 2 == 0)
- printf("Even Number");
- else
- printf("Odd Number");
- return 0;
- }
Enter a number: 8
Even Number
Output (Odd case):
Enter a number: 7
Odd Number
2. Write a program that takes year as input and check whether it is leap year or not.
- #include<stdio.h>
- int main() {
- int year;
- // Simple rule: A leap year is divisible by 4
- printf("Enter year: ");
- scanf("%d", &year);
- if(year % 4 == 0)
- printf("Leap Year");
- else
- printf("Not a Leap Year");
- return 0;
- }
Enter year: 2024
Leap Year
Output (Not a Leap Year case):
Enter year: 2023
Not a Leap Year
3. write a program that takes percentage marks of a student as input and display his grade as per below criteria.
80% to above = A
70% - 80% = B
60% - 70% = C
50% - 60% = D
below 50% = F
- #include<stdio.h>
- int main() {
- float marks;
- // Grade Criteria:
- // 80% and above = A
- // 70% - 79% = B
- // 60% - 69% = C
- // 50% - 59% = D
- // Below 50% = F
- printf("Enter percentage marks: ");
- scanf("%f", &marks);
- if(marks >= 80)
- printf("Grade = A");
- else if(marks >= 70)
- printf("Grade = B");
- else if(marks >= 60)
- printf("Grade = C");
- else if(marks >= 50)
- printf("Grade = D");
- else
- printf("Grade = F");
- return 0;
- }
Enter percentage marks: 85
Grade = A
Enter percentage marks: 75
Grade = B
Enter percentage marks: 65
Grade = C
Enter percentage marks: 55
Grade = D
Enter percentage marks: 45
Grade = F
4. Write a program that inputs 3 numbers from user and print the maximum one.
- #include<stdio.h>
- int main() {
- int a, b, c;
- // Input 3 numbers (all different) and find the largest
- printf("Enter three numbers: ");
- scanf("%d %d %d", &a, &b, &c);
- if(a > b && a > c)
- printf("Maximum = %d", a);
- else if(b > a && b > c)
- printf("Maximum = %d", b);
- else
- printf("Maximum = %d", c);
- return 0;
- }
Enter three numbers: 10 20 30
Maximum = 30
Enter three numbers: 50 25 40
Maximum = 50
Enter three numbers: 12 35 20
Maximum = 35
5. Write a program that inputs 2 numbers from user and print the maximum one.
- #include<stdio.h>
- int main() {
- int a, b;
- // Input 2 numbers (assumed different) and find the largest
- printf("Enter two numbers: ");
- scanf("%d %d", &a, &b);
- if(a > b)
- printf("Maximum = %d", a);
- else
- printf("Maximum = %d", b);
- return 0;
- }
Enter two numbers: 10 20
Maximum = 20
Enter two numbers: 35 25
Maximum = 35
6. Write a program that inputs 3 numbers from user and print the minimum one.
- #include<stdio.h>
- int main() {
- int a, b, c;
- // Input 3 numbers (all different) and find the smallest
- printf("Enter three numbers: ");
- scanf("%d %d %d", &a, &b, &c);
- if(a < b && a < c)
- printf("Smallest = %d", a);
- else if(b < a && b < c)
- printf("Smallest = %d", b);
- else
- printf("Smallest = %d", c);
- return 0;
- }
Enter three numbers: 10 20 30
Smallest = 10
Enter three numbers: 50 25 40
Smallest = 25
Enter three numbers: 12 35 20
Smallest = 12
7. Write a program that inputs 2 numbers from user and print the minimum one.
- #include<stdio.h>
- int main() {
- int a, b;
- // Input 2 numbers (assumed different) and find the smallest
- printf("Enter two numbers: ");
- scanf("%d %d", &a, &b);
- if(a < b)
- printf("Smallest = %d", a);
- else
- printf("Smallest = %d", b);
- return 0;
- }
Enter two numbers: 10 20
Smallest = 10
Enter two numbers: 35 25
Smallest = 25
8. Write a program that print sum of first ten natural numbers using for loop.
- #include<stdio.h>
- int main() {
- int i, sum = 0;
- // Sum of first 10 natural numbers using for loop
- for(i = 1; i <= 10; i++) {
- sum = sum + i;
- }
- printf("Sum of first 10 natural numbers = %d", sum);
- return 0;
- }
Sum of first 10 natural numbers = 55
9. Write a program that input a number from user and find its factorial.
- #include<stdio.h>
- int main() {
- int n, i, fact = 1;
- printf("Enter a number: ");
- scanf("%d", &n);
- // Calculate factorial using for loop
- for(i = 1; i <= n; i++) {
- fact = fact * i; // fact = fact * i
- }
- printf("Factorial of %d = %d", n, fact);
- return 0;
- }
Enter a number: 5
Factorial of 5 = 120
Enter a number: 3
Factorial of 3 = 6
10. Write a program that will input a number from user and print table of that number.
- #include<stdio.h>
- int main() {
- int n, i;
- printf("Enter a number: ");
- scanf("%d", &n);
- // Print multiplication table of the number
- for(i = 1; i <= 10; i++) {
- printf("%d x %d = %d\n", n, i, n * i);
- }
- return 0;
- }
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
11. Write a program to print first 5 even numbers.
- #include<stdio.h>
- int main() {
- int i;
- // Print first 5 even numbers
- for(i = 1; i <= 5; i++) {
- printf("%d ", 2 * i); // 2, 4, 6, 8, 10
- }
- return 0;
- }
2 4 6 8 10
12. Write a program to print numbers from 1 to 100.
- #include<stdio.h>
- int main() {
- int i;
- // Print numbers from 1 to 100
- for(i = 1; i <= 100; i++) {
- printf("%d ", i);
- }
- return 0;
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
13. Write a program to print table of 5 using for loop.
- #include<stdio.h>
- int main() {
- int i, n = 5;
- // Print multiplication table of 5
- for(i = 1; i <= 10; i++) {
- printf("%d x %d = %d\n", n, i, n * i);
- }
- return 0;
- }
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
14. Program printing even numbers from 2 to 20.
- #include<stdio.h>
- int main() {
- int i;
- // Print first 10 even numbers using 2*i
- for(i = 1; i <= 10; i++) {
- printf("%d ", 2 * i);
- }
- return 0;
- }
2 4 6 8 10 12 14 16 18 20
15. Write a program that will input a number from user and check whether it is positive, negative or zero.
- #include<stdio.h>
- int main() {
- int num;
- // Input a number and check whether it is positive, negative, or zero
- printf("Enter a number: ");
- scanf("%d", &num);
- if(num > 0)
- printf("Positive Number");
- else if(num < 0)
- printf("Negative Number");
- else
- printf("Zero");
- return 0;
- }
Case 1: num > 0
Enter a number: 10
Positive Number
Case 2: num < 0
Enter a number: -5
Negative Number
Case 3: num = 0
Enter a number: 0
Zero