Least Important Programs

Note: You can skip comments mentioned in the code during writing. No need to write them on paper. It is a professional practice to add comments but right now you can skip dear students. And You can even skip outputs. These are just added to guide you properly that how the programs will work.

1. Write a program that takes radius of a circle as input. The program should calcualte and display the area of a circle.

  1. #include<stdio.h>
  2. int main() {
  3. float radius, area;
  4. // Formula: Area = π × r × r
  5. printf("Enter radius: ");
  6. scanf("%f", &radius);
  7. area = 3.1416 * radius * radius;
  8. printf("Area of circle = %f", area);
  9. return 0;
  10. }
Output:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int age;
  4. // If age is between 13 and 19, person is a teenager
  5. printf("Enter age: ");
  6. scanf("%d", &age);
  7. if(age >= 13 && age <= 19)
  8.       printf("Teenager");
  9. else
  10.       printf("Not a Teenager");
  11. return 0;
  12. }
Output (If case):

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.

  1. #include<stdio.h>
  2. int main() {
  3. int num1, num2;
  4. // A number is a factor of another if second number % first number == 0
  5. printf("Enter two numbers: ");
  6. scanf("%d %d", &num1, &num2);
  7. if(num2 % num1 == 0)
  8.       printf("%d is a factor of %d", num1, num2);
  9. else
  10.       printf("%d is not a factor of %d", num1, num2);
  11. return 0;
  12. }
Output Examples:

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

  1. #include<stdio.h>
  2. int main() {
  3. // Hard-coded ages of 5 persons
  4. int ages[5] = {18, 21, 16, 25, 19};
  5. // Display ages without using loop
  6. printf("Ages of persons are: %d %d %d %d %d",
  7. ages[0], ages[1], ages[2], ages[3], ages[4]);
  8. return 0;
  9. }
Output:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int num;
  4. // If a number is divisible by 2, it is Even; otherwise Odd
  5. printf("Enter a number: ");
  6. scanf("%d", &num);
  7. if(num % 2 == 0)
  8.       printf("Even Number");
  9. else
  10.       printf("Odd Number");
  11. return 0;
  12. }
Output (Even case):

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.

  1. #include<stdio.h>
  2. int main() {
  3. int year;
  4. // Simple rule: A leap year is divisible by 4
  5. printf("Enter year: ");
  6. scanf("%d", &year);
  7. if(year % 4 == 0)
  8.       printf("Leap Year");
  9. else
  10.       printf("Not a Leap Year");
  11. return 0;
  12. }
Output (Leap Year case):

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

  1. #include<stdio.h>
  2. int main() {
  3. float marks;
  4. // Grade Criteria:
  5. // 80% and above = A
  6. // 70% - 79% = B
  7. // 60% - 69% = C
  8. // 50% - 59% = D
  9. // Below 50% = F
  10. printf("Enter percentage marks: ");
  11. scanf("%f", &marks);
  12. if(marks >= 80)
  13.       printf("Grade = A");
  14. else if(marks >= 70)
  15.       printf("Grade = B");
  16. else if(marks >= 60)
  17.       printf("Grade = C");
  18. else if(marks >= 50)
  19.       printf("Grade = D");
  20. else
  21.       printf("Grade = F");
  22. return 0;
  23. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int a, b, c;
  4. // Input 3 numbers (all different) and find the largest
  5. printf("Enter three numbers: ");
  6. scanf("%d %d %d", &a, &b, &c);
  7. if(a > b && a > c)
  8.       printf("Maximum = %d", a);
  9. else if(b > a && b > c)
  10.       printf("Maximum = %d", b);
  11. else
  12.       printf("Maximum = %d", c);
  13. return 0;
  14. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int a, b;
  4. // Input 2 numbers (assumed different) and find the largest
  5. printf("Enter two numbers: ");
  6. scanf("%d %d", &a, &b);
  7. if(a > b)
  8.       printf("Maximum = %d", a);
  9. else
  10.       printf("Maximum = %d", b);
  11. return 0;
  12. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int a, b, c;
  4. // Input 3 numbers (all different) and find the smallest
  5. printf("Enter three numbers: ");
  6. scanf("%d %d %d", &a, &b, &c);
  7. if(a < b && a < c)
  8.       printf("Smallest = %d", a);
  9. else if(b < a && b < c)
  10.       printf("Smallest = %d", b);
  11. else
  12.       printf("Smallest = %d", c);
  13. return 0;
  14. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int a, b;
  4. // Input 2 numbers (assumed different) and find the smallest
  5. printf("Enter two numbers: ");
  6. scanf("%d %d", &a, &b);
  7. if(a < b)
  8.       printf("Smallest = %d", a);
  9. else
  10.       printf("Smallest = %d", b);
  11. return 0;
  12. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int i, sum = 0;
  4. // Sum of first 10 natural numbers using for loop
  5. for(i = 1; i <= 10; i++) {
  6.       sum = sum + i;
  7. }
  8. printf("Sum of first 10 natural numbers = %d", sum);
  9. return 0;
  10. }
Output:

Sum of first 10 natural numbers = 55

9. Write a program that input a number from user and find its factorial.

  1. #include<stdio.h>
  2. int main() {
  3. int n, i, fact = 1;
  4. printf("Enter a number: ");
  5. scanf("%d", &n);
  6. // Calculate factorial using for loop
  7. for(i = 1; i <= n; i++) {
  8.       fact = fact * i; // fact = fact * i
  9. }
  10. printf("Factorial of %d = %d", n, fact);
  11. return 0;
  12. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int n, i;
  4. printf("Enter a number: ");
  5. scanf("%d", &n);
  6. // Print multiplication table of the number
  7. for(i = 1; i <= 10; i++) {
  8.       printf("%d x %d = %d\n", n, i, n * i);
  9. }
  10. return 0;
  11. }
Output Examples:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int i;
  4. // Print first 5 even numbers
  5. for(i = 1; i <= 5; i++) {
  6.       printf("%d ", 2 * i); // 2, 4, 6, 8, 10
  7. }
  8. return 0;
  9. }
Output:

2 4 6 8 10

12. Write a program to print numbers from 1 to 100.

  1. #include<stdio.h>
  2. int main() {
  3. int i;
  4. // Print numbers from 1 to 100
  5. for(i = 1; i <= 100; i++) {
  6.       printf("%d ", i);
  7. }
  8. return 0;
  9. }
Output:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int i, n = 5;
  4. // Print multiplication table of 5
  5. for(i = 1; i <= 10; i++) {
  6.       printf("%d x %d = %d\n", n, i, n * i);
  7. }
  8. return 0;
  9. }
Output:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int i;
  4. // Print first 10 even numbers using 2*i
  5. for(i = 1; i <= 10; i++) {
  6.       printf("%d ", 2 * i);
  7. }
  8. return 0;
  9. }
Output:

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.

  1. #include<stdio.h>
  2. int main() {
  3. int num;
  4. // Input a number and check whether it is positive, negative, or zero
  5. printf("Enter a number: ");
  6. scanf("%d", &num);
  7. if(num > 0)
  8.       printf("Positive Number");
  9. else if(num < 0)
  10.       printf("Negative Number");
  11. else
  12.       printf("Zero");
  13. return 0;
  14. }
Output Examples:

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