C Programming Exercises- Practice Questions with Solutions

1. Write a C Program to check two numbers are equal or not.

Solution:

Output:

Numbers are equal

OR

Output:

Numbers are not equal

2. Write a C program to accept two numbers and check whether they are equal or not.

Solution:

Output-1:

Enter 1st number: 34

Enter 2nd number: 34

Numbers are equal

Output-2:

Enter 1st number: 12

Enter 2nd number: 23

Numbers are not equal

OR

Output-1:

Enter two numbers: 45 45

Numbers are equal

Output-2:

Enter two numbers: 23 12

Numbers are not equal

3. Write a C program to find the larger number among two numbers.

OR

Write a program in C to find the maximum number between two numbers.

Solution:

OR

Output:

Enter 2 numbers: 23 34

Larger number :34

4. Write a program in C to check whether a number is even or odd.

Solution:

Output-1:

Enter a number: 4

4 is an even number

Output-2:

Enter a number: 5

5 is an odd number

5. Write a C program to check whether a year is leap year or not.

Solution:

Output-1:

Enter a year: 2020

2020 is a leap year

Output-2:

Enter a year: 2025

2025 is not a leap year

6. Write a program in C to find the greatest number among three numbers.

Solution:

OR

Output:

Enter three numbers: 12 24 46

The greatest number is 46

7. Write a C program to classify a person’s age group and determine if they are eligible to vote. A person under 18 is a minor, between 18 and 60 is an adult and above 60 is a senior. Only adults and seniors are eligible to vote.

Solution:

Output-1:

Enter age: 15

Category: Minor

Not eligible to vote

Output-2:

Enter age: 50

Category: Adult

Eligible to vote

Output-3:

Enter age: 70

Category: Senior

Eligible to vote

8. Write a C program to input a number between 1-7 and print the corresponding day of a week (day name).

Solution:

Output-1:

Enter a number (1-7): 5

Thursday

Output-2:

Enter a number (1-7): 9

Invalid Number

9. Write a program in C to input marks of three subjects of a student. Calculate total marks and percentage and display grade. The student get grade as per the following conditions:

Percentage         Grade

Above or equal to 90                        A+

Between 80 and 89                           A

Between 70 and 79                           B

Between 60 and 69                           C

Between 50 and 59                           D

Less than  50                                      Fail

Solution:

Output:

Enter english marks: 87

Enter maths marks: 90

Enter science marks: 88

———————————

Total marks obtained: 265.00

Percentage: 88.33

Grade A

10. Write a program in C to determines loan eligibility using nested if statement based on age and income criteria.

Solution:

Output-1

Enter your age: 30

Enter your monthly income: 35000

You are eligible for loan

Output-2

Enter your age: 30

Enter your monthly income: 28000

Income insufficient for loan eligibility

Output-3

Enter your age: 20

Enter your monthly income: 30000

You are not eligible for the loan due to age

11. Write a C program to determines driving eligibility based on age and whether the individual has a learner’s permit, using nested if statement.

Solution:

Output-1

Enter your age: 18

Do you have a learner’s permit? (Y/N): Y

You are eligible for driving practice

Output-2

Enter your age: 17

Do you have a learner’s permit? (Y/N): N

You are eligible for a learner’s permit, but not for driving

Output-3

Enter your age: 12

You are not yet eligible for a learner’s permit or driving

12. Write a C program to check whether a character is vowel or not using switch case.

Solution:

OR

Output-1:

Enter any character: a

a is a vowel

Output-2:

Enter any character: U

U is a vowel

Output-3:

Enter any character: R

R is not a vowel

13. Write a program in C to print day of a week using switch case.

Solution:

Output-1:

Enter a number (1-7) for the day of the week: 5

Thursday

Output-2:

Enter a number (1-7) for the day of the week: 12

Invalid Input

Please, enter a number between 1 and 7

14. Write a C program to print 1st 100 natural numbers.

OR

Write a program in C to print the number from 1 to 100.

Solution:

Output:

First 100 natural numbers:

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

15. Write a C program to print numbers from 100 to 1 in reverse order.

Solution:

Output:

Numbers from 100 to 1:

100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

16. Write a program in C to print numbers from 10 to 80 with a gap of 2.

Solution:

Output:

Numbers from 10 to 80 with gap 2:

10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80

17. Write a C program to print alphabet from a to z.

Solution:

Output:

Alphabet from a to z:

a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z

18. Write a program in C to print all even numbers between 1 and 50.

Solution:

Output:

Even numbers between 1 and 50:

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

19. Write a C program to find the sum of first n natural numbers.

Solution:

Output:

Enter a positive integer: 5

Sum of first 5 natural numbers: 15

20. Write a C program to print multiplication table.

Solution:

Output:

Enter a number: 4

Multiplication Table for 4:

—————————

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

21. Write a C program to print multiplication table from 1 to 5.

Solution:

Output:

Multiplication Table from 1 to 5:

——————————–

22. Write a C pattern program for Right-Angle Triangle.(Right Half Pyramid Pattern program in C)

Solution:

Output:

Enter the number of rows: 8

23. Write a program in C to print the following pattern.(Half Pyramid of Numbers)

Solution:

Output:

Enter the number of rows: 9

24. Write a program in C to print the following pattern. (Half Pyramid of Numbers)

Solution:

Output:

Enter the number of rows: 9

25. Write a C program to print all natural numbers from 1 to n using while loop.

Solution:

Output:

Enter a number: 20

Natural numbers from 1 to 20:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

26. Write a program in C to print numbers from 10 to 1 in reverse order using while loop.

Solution:

Output:

Numbers from 10 to 1:

10 9 8 7 6 5 4 3 2 1

27. Write a program in C to accept only positive integer as input from the user.

Solution:

Output:

Enter a positive number: -5

You entered -5

Invalid input!

Please enter positive number: -2

You entered -2

Invalid input!

Please enter positive number: 4

OR

Output:

Enter a positive number: -6

Invalid input!

Enter a positive number: -5

Invalid input!

Enter a positive number: 23

You entered 23

28. Write a C program to print your name 5 times using do-while loop.

Solution:

Output:

Eliza

Eliza

Eliza

Eliza

Eliza

29. Write a program in C for asking user to input a positive number and when user enters 0 or a negative number the loop should stop.

Solution:

Output:

Enter a positive number: 5

Enter a positive number: 4

Enter a positive number: 2

Enter a positive number: 6

Enter a positive number: 0

30. Write a C program to print the elements of an array.

Solution:

Output:

Array elements are: 10 20 30 40 50

31. Write a C program to print the elements of an array using for loop.

Solution:

Output:

Array elements are: 10 20 30 40 50

32. Write a program in C to read and print the elements of an array.

Solution:

Output:

Enter 5 array elements:12 23 34 45 43

The array elements are:12 23 34 45 43

33. Write a C program to print the elements of a two-dimensional (2D) array.

Solution:

Output:

Array elements are:

1 2 5

3 4 1

34. Write a program in C to read and print the elements of a two-dimensional (2D) array.

Solution:

Output:

Enter 9 array elements: 5 3 4 2 7 1 4 6 2

Array elements are:

5 3 4

2 7 1

4 6 2

Leave a Comment