31 C Programming Exercises and Solutions


If you know the fundamentals of C programming, now you can focus on solving some coding questions to practice. You will be able to understand all the basic concepts of C programming if you solve some coding problems.

In this post, I have put together some C programming problems that you can use for practice. I have also provided the C code solutions and the corresponding output for your reference.

Try to solve these coding challenges by yourself and get better at C programming language. Let’s dive right in.

1. C program to check whether the given number is even or odd

#include <stdio.h>

int main() {
  int myNumber;
  printf("Enter a number: ");
  scanf("%d",&myNumber);

  int x = myNumber%2;
  if(x==0){
    printf("The number is Even. \n");
  }else{
    printf("The number is Odd. \n");
  }
  
  return 0;
}

Output:

C program to check whether the given number is even or odd - output
C program to check whether the given number is even or odd - output

2. C program to convert the temperature in Degree Centigrade to Fahrenheit

#include <stdio.h>

int main() {
  int c;
  printf("Enter temperature in Centigrade: ");
  scanf("%d",&c);

  float f = ((9*c)/5)+32;
  printf("Temperature in Fahrenheit is: %f \n",f);
  
  return 0;
}

Output:

C program to convert the temperature in Degree Centigrade to Fahrenheit - output

3. C program to find the area of a triangle whose three sides are given

#include <stdio.h>
#include <math.h>

int main() {
  int a,b,c;
  printf("Enter the first side: ");
  scanf("%d",&a);
  printf("Enter the second side: ");
  scanf("%d",&b);
  printf("Enter the third side: ");
  scanf("%d",&c);

  if((a+b)>c && (a+c)>b && (b+c)>a){
    double s=(a+b+c)/2.0;
    double area=sqrt(s*(s-a)*(s-b)*(s-c));
    printf("Area of the triangle is: %f \n", area);    
  }else  
    printf("Area of the triangle does not exist. \n");
  
  return 0;
}

Output:

C program to find the area of a triangle whose three sides are given - output

4. C program to find out the average of a set of integers

#include <stdio.h>

int main() {
  int count;
  printf("Enter the count of numbers: ");
  scanf("%d",&count);

  int i = 0;
  float sum = 0;
  for(i=0;i<count;i++){
    printf("Enter an integer: ");
    int x;
    scanf("%d",&x);
    sum = sum + x;
  }

  float avg = sum/count;
  printf("The average is: %f \n",avg);
  
  return 0;
}

Output:

C program to find out the average of a set of integers - output

5. C program to find the product of a set of real numbers

#include <stdio.h>

int main() {
  int count;
  printf("Enter the count of real numbers: ");
  scanf("%d",&count);

  int i = 0;
  float product = 1.0;
  for(i=0;i<count;i++){
    printf("Enter a real number: ");
    float x;
    scanf("%f",&x);
    product = product * x;
  }

  printf("The product of the numbers is: %f \n",product);
  
  return 0;
}

Output:

C program to find the product of a set of real numbers - output

6. C program to find the circumference and area of a circle with a given radius

#include <stdio.h>
#include <math.h>

int main() {
  double radius;
  printf("Enter the radius of the circle: ");
  scanf("%lf",&radius);

  double c = 2 * M_PI * radius;
  double area = M_PI * radius * radius;

  printf("The circumference of the circle is: %lf \n",c);
  printf("The area of the circle is: %lf \n",area);
  
  
  return 0;
}

Output:

C program to find the circumference and area of a circle with a given radius - output

7. C program to check whether the given integer is a multiple of 5

#include <stdio.h>

int main() {
  int myNumber;
  printf("Enter a number: ");
  scanf("%d",&myNumber);

  if(myNumber%5==0){
    printf("%d is a multiple of 5. \n",myNumber);
  }else{
    printf("%d is not a multiple of 5. \n",myNumber);
  }
  
  return 0;
}

Output:

C program to check whether the given integer is a multiple of 5 - output
C program to check whether the given integer is a multiple of 5 - output2

8. C program to check whether the given integer is a multiple of both 5 and 7

#include <stdio.h>

int main() {
  int myNumber;
  printf("Enter a number: ");
  scanf("%d",&myNumber);

  if((myNumber%5==0)&&(myNumber%7==0)){
    printf("%d is a multiple of both 5 and 7. \n",myNumber);
  }else{
    printf("%d is not a multiple of both 5 and 7. \n",myNumber);
  }
  
  return 0;
}

Output:

C program to check whether the given integer is a multiple of both 5 and 7 - output
C program to check whether the given integer is a multiple of both 5 and 7 - output2

9. C program to find the average of 5 numbers using while loop

#include <stdio.h>

int main() {
  int count = 0;
  int sum = 0;

  while(count<5){
    printf("Enter an integer: \n");
    int number;
    scanf("%d",&number);
    sum = sum+number;
    count++;
  }

  double avg = (double)sum/(double)count;
  printf("The average is : %lf",avg);
  
  return 0;
}

Output:

C program to find the average of 5 numbers using while loop - output

10. C program to display the given integer in a reverse manner

#include <stdio.h>

int main() {
  int myNumber;
  printf("Enter a number: ");
  scanf("%d",&myNumber);

  int rev = 0;
  while(myNumber!=0){
    int digit = myNumber%10;
    rev = (rev*10)+digit;
    myNumber = myNumber/10;
  }
  printf("The reverse of the number is %d \n",rev);
  
  return 0;
}

Output:

C program to display the given integer in reverse manner - output

11. C program to find the geometric mean of n numbers

#include <stdio.h>
#include <math.h>

int main() {
  int count, c = 0;
  double p = 1.0;
  printf("Enter the number of values: ");
  scanf("%d",&count);

  while((c<count)){
    printf("Enter a real number: ");
    double x;
    scanf("%lf",&x);
    c = c+1;
    p = p * x;
  }

  double gm = pow(p,1.0/count);
  printf("The geometric mean is: %lf",gm);
  
  return 0;
}

Output:

C program to find the geometric mean of n numbers - output

12. C program to find the sum of the digits of an integer using a while loop

#include <stdio.h>

int main() {
  int number, sum = 0;
  printf("Enter an integer: ");
  scanf("%d",&number);

  while(number!=0){
    int digit = number%10;
    sum = sum+digit;
    number = number/10;
  }

  printf("Sum of digits is: %d \n", sum);
  
  return 0;
}

Output:

C program to find the sum of the digits of an integer using while loop - output

13. C program to display all the multiples of 3 within the range 10 to 50

#include <stdio.h>

int main() {

  for(int i=10;i<50;i++){
    if(i%3==0)
      printf("%d \n",i);
  }
  
  return 0;
}

Output:

C program to display all the multiples of 3 within the range 10 to 50 - output

14. C program to display all integers within the range 100-150 whose sum of digits is an even number

#include <stdio.h>

int main() {

  for(int i=100;i<150;i++){
    int num = i;
    int sum = 0;
    while(num!=0){
      int digit = num%10;
      sum = sum + digit;
      num = num/10;
    }
    
    if(sum%2==0){
      printf("%d \n",i);
    }
  }
  
  return 0;
}

Output:

C program to display all integers within the range 100-150 whose sum of digits is an even number - output

15. C program to check whether the given integer is a prime number or not

#include <stdio.h>

int main() {

  printf("Enter an integer greater than 1: ");
  int number;
  scanf("%d",&number);

  int isprime = 1;

  for(int i=2;i<(number/2);i++){
    if(number%i==0){
      isprime = 0;
      break;
    }
  }

  if(isprime==1){
    printf("%d is a prime number",number);
  }else{
      printf("%d is not a prime number",number);
  }
    
  return 0;
}

Output:

C program to check whether the given integer is a prime number or not - output
C program to check whether the given integer is a prime number or not - output2

16. C program to generate the prime numbers from 1 to N

#include <stdio.h>

int main() {

  int number;
  printf("Enter range: ");
  scanf("%d",&number);

  for (int num = 2; num <= number; num++){
    int isPrime = 1;
    for (int i=2; i <= num/2; i++){
      if( num % i == 0){
        isPrime = 0;
        break;
      }
    }

    if (isPrime == 1)
      printf("%d \n",num);
    
  }
    
  return 0;
}

Output:

17. C program to print the numbers from a given number n till 0 using recursion

#include <stdio.h>
void print_till_zero();

int main() {

  print_till_zero(7);
    
  return 0;
}

void print_till_zero(int n){
  if(n==0)
    return;
  printf("%d \n",n);
  n=n-1;
  print_till_zero(n);
}

Output:

18. C program to find the factorial of a number using recursion

#include <stdio.h>
int fact();

int main() {

  printf("%d \n",fact(6));
    
  return 0;
}

int fact(int n){
  int f;
  if(n==1)
    f=1;
  else
    f = n * fact(n-1);
  return f;
}

Output:

19. C program to display the sum of n numbers using an array

#include <stdio.h>

int main() {

  int count;
  printf("Enter the count of numbers: ");
  scanf("%d",&count);

  int numbers[count];
  int sum=0;
  
  for(int i=0;i<count;i++){
    printf("Enter the number: ");
    int x;
    scanf("%d",&x);
    numbers[i] = x;
  }

  for(int i = 0; i < count; i++) {  
      sum = sum + numbers[i];  
  }

  printf("Sum of all the elements of the array: %d \n", sum);
  
  return 0;
}

Output:

C program to display the sum of n numbers using an array output

20. C program to find the number of odd numbers in an array

#include <stdio.h>

int main() {

  int numbers[] = {8,3,1,6,2,4,5,9};
  int count = 0;

  int size = sizeof numbers / sizeof numbers[0];

  for(int i=0;i<size;i++){
    if(numbers[i]%2!=0)
      count++;
  }
  
  printf("The count of odd numbers in the list is: %d \n",count);    
  return 0;
}

Output:

21. C program to find the largest number in an array without using built-in functions

#include <stdio.h>

int main() {

  int numbers[] = {3,8,1,7,2,9,5,4};
  int largest = numbers[0];
  int position = 0;

  int size = sizeof numbers / sizeof numbers[0];

  for(int i=0;i<size;i++){
    if(numbers[i]>largest){
      largest = numbers[i];
      position = i;
    }
  }

  printf("The largest element is %d which is found at position %d \n",largest,position);
  
  return 0;
}

Output:

22. C program to insert a number to any position in an array

#include <stdio.h>

int main() {

  int x,y;
  int numbers[] = {3,4,1,9,6,2,8}; 
  int size = sizeof numbers / sizeof numbers[0];
  
  for(int i=0;i<size;i++){
    printf("%d ",numbers[i]);
  }

  printf("\nEnter the number to be inserted: ");
  scanf("%d",&x);
  printf("Enter the position: ");
  scanf("%d",&y);
  
  for(int i=size-1;i>y;i--){
      numbers[i] = numbers[i-1];
  }
  numbers[y] = x;

  for(int i=0;i<size;i++){
    printf("%d ",numbers[i]);
  }
  
  return 0;
}

Output:

23. C program to check whether a number is palindrome or not

#include <stdio.h>  
  
int main()  
{  
  int n,r,sum=0,temp;    
  printf("Enter a number: ");    
  scanf("%d",&n);    
  temp=n;
  
  while(n>0){    
    r=n%10;    
    sum=(sum*10)+r;    
    n=n/10;    
  }    
  if(temp==sum)    
    printf("It's a palindrome number.");    
  else    
    printf("It's not a palindrome number.");  
  
    return 0;  
}  

Output:

24. C program to implement matrix addition

#include <stdio.h>  
  
int main()  
{  
  //creating two matrices    
  int a[3][3]={{8,5,1},{9,3,2},{4,6,3}};    
  int b[3][3]={{8,5,3},{9,5,7},{9,4,1}};    
    
  //defining the matrix to store the sum of two matrices    
  int c[3][3];
    
    //adding 2 matrices    
    for(int i=0;i<3;i++){    
      for(int j=0;j<3;j++){    
        c[i][j]=a[i][j]+b[i][j];
        printf("%d ",c[i][j]);    
      }
      printf("\n"); 
    }    
  
    return 0;  
}  

Output:

C  program to implement matrix addition - output

25. C program to implement matrix multiplication

#include <stdio.h>  
  
int main()  
{  
  //creating two matrices    
  int a[3][3]={{8,5,1},{9,3,2},{4,6,3}};    
  int b[3][3]={{8,5,3},{9,5,7},{9,4,1}};    
    
  //defining the matrix to store the product of two matrices    
  int c[3][3];
    
    //multiplying 2 matrices
    for(int i=0;i<3;i++){    
      for(int j=0;j<3;j++){    
        c[i][j]=0;      
        for(int k=0;k<3;k++){      
          c[i][j]+=a[i][k]*b[k][j];      
        }
        printf("%d ",c[i][j]);
      }
      printf("\n"); 
    }  
  
    return 0;  
}  

Output:

26. C program to check leap year

#include <stdio.h>  
  
int main()  
{  
    printf("Enter the year you want to check: ");
    int year;
    scanf("%d",&year);
    int leap = 0;

    // if the year is divided by 4
    if (year % 4 == 0) {
      // if the year is century
      if (year % 100 == 0) {
        // if year is divided by 400, then it is a leap year
        if (year % 400 == 0)
          leap = 1;
        else
          leap = 0;
      }
      // if the year is not century
      else
        leap = 1;
    }
    else
      leap = 0;

    if(leap==1)
      printf("%d is a leap year.",year);
    else
      printf("%d is not a leap year.",year);
  
    return 0;  
}  

Output:

27. C program to find the Nth term in a Fibonacci series using recursion

#include <stdio.h>  
  
int main()  
{  
  int n;
  printf("Enter the position(N): ");
  scanf("%d",&n);
  printf("\nNth Fibonacci Number is: %d",NthFibonacciNumber(n));
  
    return 0;  
}  

int NthFibonacciNumber(int n){
  if(n==1)
      return 0;
  else if(n==2)
      return 1;
  else
      return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}

Output:

28. C program to print the Fibonacci series using iteration

#include <stdio.h>  
  
int main()  
{  
  int n1=0,n2=1;
  printf("Enter the number of terms in the sequence: ");
  int count;
  scanf("%d",&count);
  int n3,i;

  //printing 0 and 1
  printf("%d %d ",n1,n2);
  
  //printing from 2 because 0 and 1 are already printed  
  for(i=2;i<count;++i){
    n3=n1+n2;    
    printf("%d ",n3);    
    n1=n2;    
    n2=n3;
  }    
  
    return 0;  
}  

int NthFibonacciNumber(int n){
  if(n==1)
      return 0;
  else if(n==2)
      return 1;
  else
      return NthFibonacciNumber(n-1)+NthFibonacciNumber(n-2);
}

Output:

 C program to print the Fibonacci series using iteration

29. C program to implement a calculator to do basic arithmetic operations

#include <stdio.h>  
  
int main()  
{  

  int firstNumber, secondNumber, opt, add, sub, mul;
  double div;
  printf("Enter first number: ");
  scanf("%d",&firstNumber);
  printf("Enter second number: ");
  scanf("%d",&secondNumber);

  printf("Enter 1 for addition \n");
  printf("Enter 2 for subtraction \n");
  printf("Enter 3 for multiplication \n");
  printf("Enter 4 for division \n");
  printf("Enter 5 to Exit \n");
  
  int option;
  scanf("%d",&option);
  switch(option){
    case 1:
      add = firstNumber + secondNumber;
      printf("Result: %d",add);
      break;

    case 2:
      sub = firstNumber - secondNumber;
      printf("Result: %d",sub);
      break;

    case 3:
      mul = firstNumber * secondNumber;
      printf("Result: %d",mul);
      break;

    case 4:
      div = (double)firstNumber / secondNumber;
      printf("Result: %lf",div);
      break;    

    case 5:
      exit(0);
      
      }
  
}

Output:

C program to implement a calculator to do basic operations - output

30. C Program to Find the Sum of Even Numbers

#include <stdio.h>  
  
int main()  
{  

  int sum=0;
    
  for(int i=1;i<=100;i++){
    if(i%2==0){
      sum = sum + i;
    }
  }

  printf("The sum of even numbers from 1-100 is: %d",sum);
  
}

Output:

31. C program to implement linear search

#include <stdio.h>  
  
int main()  
{  

  int numbers[] = {4,2,7,1,8,3,6};
  int flag = 0;
    
  printf("Enter the number to be found out: ");
  int x;
  scanf("%d",&x);

  int size = sizeof numbers/ sizeof numbers[0];
    
  for(int i=0;i<size;i++){
    if (x==numbers[i]){
      printf("Successful search, the element is found at position %d", i);
      flag = 1;
      break;
    }
  }

    if(flag==0){
      printf("Oops! Search unsuccessful");
    }
  
}

Output:

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

One thought on “31 C Programming Exercises and Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts