Loop Program in C Exercises with Solutions & Explanation

After diving in through the loop and its types (for loop, while loop, do-while loop), let’s go for the loop program in C with some advanced programs and their explanation.

In this tutorial, we will cover some important questions with their solutions and explanation as well. So let’s start without wasting any time.

Terms you may encounter in question

WAP: Write a Program

1. WAP in C to display the first 10 natural numbers. 

#include <stdio.h>
void main()
{     
    int i;
	printf("The very first 10 natural numbers are:");
	for (i=1;i<=10;i++)
	{      
		printf("%d ",i);
	}
printf("\n");
}

Output

The very first 10 natural numbers are:1 2 3 4 5 6 7 8 9 10

 

2. WAP to find the sum of the first 10 natural numbers. 

#include <stdio.h>
void main()
{
    int  i, sum = 0;
    //Displaying first 10 natural numbers
    printf("The first 10 natural numbers are : ");
 
    for (i = 1; i <= 10; i++)
    {
        printf("%d ",i); 
        sum = sum + i;    //Storing sum of the numbers
    }
    printf("\nSum of the first 10 natural numbers : %d\n", sum);
}

Output

The first 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10                                                                                          
Sum of the first 10 natural numbers : 55

3. WAP in C to display n terms of natural number and their sum.

#include <stdio.h>
void main()
{
   int i,n,sum=0;
   printf("Input Value of terms : ");
   scanf("%d",&n);
   printf("\nThe first %d natural numbers are:\n",n);
   for(i=1;i<=n;i++)
   {
     printf("%d ",i);
     sum+=i;
   }
   printf("\nThe Sum of natural numbers upto %d terms : %d \n",n,sum);

}

Output

Input Value of terms : 7                                                                                      
                                                                                                              
The first 7 natural number is :                                                                               
1 2 3 4 5 6 7                                                                                                 
The Sum of Natural Number upto 7 terms : 28 

4. WAP in C to read ten numbers from the keyboard and find their sum and average. 

#include <stdio.h>
void main()
{       
    int i,n,sum=0;
	float avg;
	printf("Input the 10 numbers : \n");
	for (i=1;i<=10;i++)
	{
                printf("Number-%d :",i);

		scanf("%d",&n);
		sum +=n;
	}
	avg=sum/10.0;
	printf("The sum of 10 no is : %d\nThe Average is : %f\n",sum,avg);
 
}

Output

Input the 10 numbers :                                                                                        
Number-1 :1                                                                                                   
Number-2 :2                                                                                                   
Number-3 :3                                                                                                   
Number-4 :4                                                                                                   
Number-5 :5                                                                                                   
Number-6 :6                                                                                                   
Number-7 :7                                                                                                   
Number-8 :8                                                                                                   
Number-9 :9                                                                                                   
Number-10 :10                                                                                                 
The sum of 10 no is : 55                                                                                      
The Average is : 5.500000  

5. WAP in C to display the cube of the number up to given an integer. 

Example :

The number is: 1 and the cube of the 1 is :1

The number is: 2 and the cube of the 2 is:8

The number is : 3 and the cube of the 3 is:27

The number is: 4 and the cube of the 4 is:64

The number is: 5 and the cube of the 5 is:125

#include <stdio.h>
void main()
 {
    int i,ctr;
    printf("Input number of terms : ");
    scanf("%d", &ctr);
    for(i=1;i<=ctr;i++)
    {
	 printf("Number is : %d and cube of the %d is :%d \n",i,i, (i*i*i));     
    }
 }

Output

Input number of terms : 5                                                                                     
Number is : 1 and cube of the 1 is :1                                                                         
Number is : 2 and cube of the 2 is :8                                                                         
Number is : 3 and cube of the 3 is :27                                                                        
Number is : 4 and cube of the 4 is :64                                                                        
Number is : 5 and cube of the 5 is :125

6. WAP in C to display the multiplication table of a given integer. 

Example :

5 X 1 = 5

5 X 10 = 50

#include <stdio.h>
void main()
{
   int j,n;
   printf("Input the number (Table to be calculated) : ");
   scanf("%d",&n);
   printf("\n");
   for(j=1;j<=10;j++)
   {
     printf("%d X %d = %d \n",n,j,n*j);
   }
} 

Output

Input the number (Table to be calculated) : 15                                                                
                                                                                                              
15 X 1 = 15                                                                                                   
15 X 2 = 30                                                                                                   
15 X 3 = 45                                                                                                   
15 X 4 = 60                                                                                                   
15 X 5 = 75                                                                                                   
15 X 6 = 90                                                                                                   
15 X 7 = 105                                                                                                  
15 X 8 = 120                                                                                                  
15 X 9 = 135                                                                                                  
15 X 10 = 150

7. WAP in C to display the multiplication table vertically from 1 to n. 

Example :

Multiplication table from 1 to 5

1×1 = 1, 2×1 = 2, 3×1 = 3, 4×1 = 4, 5×1 = 5…

1×10 = 10, 2×10 = 20, 3×10 = 30, 4×10 = 40, 5×10 = 50 

#include <stdio.h>
void main()
{
   int j,i,n;
   printf("Input upto the table number starting from 1 : ");
   scanf("%d",&n);
   printf("Multiplication table from 1 to %d \n",n);
   for(i=1;i<=10;i++)
   {
     for(j=1;j<=n;j++)
     {
       if (j<=n-1)
           printf("%dx%d = %d, ",j,i,i*j);
          else
	    printf("%dx%d = %d",j,i,i*j);

      }
     printf("\n");
    }
} 

Output

Input upto the table number starting from 1 : 5                                                              
Multiplication table from 1 to 8                                                                              
1x1 = 1, 2x1 = 2, 3x1 = 3, 4x1 = 4, 5x1 = 5                                       
1x2 = 2, 2x2 = 4, 3x2 = 6, 4x2 = 8, 5x2 = 10
1x3 = 3, 2x3 = 6, 3x3 = 9, 4x3 = 12, 5x3 = 15
1x4 = 4, 2x4 = 8, 3x4 = 12, 4x4 = 16, 5x4 = 20
1x5 = 5, 2x5 = 10, 3x5 = 15, 4x5 = 20, 5x5 = 25
1x6 = 6, 2x6 = 12, 3x6 = 18, 4x6 = 24, 5x6 = 30
1x7 = 7, 2x7 = 14, 3x7 = 21, 4x7 = 28, 5x7 = 35
1x8 = 8, 2x8 = 16, 3x8 = 24, 4x8 = 32, 5x8 = 40
1x9 = 9, 2x9 = 18, 3x9 = 27, 4x9 = 36, 5x9 = 45
1x10 = 10, 2x10 = 20, 3x10 = 30, 4x10 = 40, 5x10 = 50

8. WAP in C to display the n terms of odd natural number and their sum. 

Example :

The odd numbers are :1 3 5 7 9 11 13 15 17 19

The Sum of odd Natural Number up to 10 terms: 100

#include <stdio.h>
void main()
{
   int i,n,sum=0;

   printf("Input number of terms : ");
   scanf("%d",&n);
   printf("\nThe odd numbers are :");
   for(i=1;i<=n;i++)
   {
     printf("%d ",2*i-1);
     sum+=2*i-1;
   }
   printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);
}

Output

Input number of terms : 10                                                                                    
                                                                                                              
The odd numbers are :1 3 5 7 9 11 13 15 17 19                                                                 
The Sum of odd Natural Number upto 10 terms : 100 

9. WAP in C to display the pattern like a right angle triangle using an asterisk. 

Pattern Example
*
**
***
****
*****
#include <stdio.h>
void main()
{
   int i,j,rows;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   for(i=1;i<=rows;i++)
   {
	for(j=1;j<=i;j++)
	   printf("*");
	printf("\n");
   }
}
Input number of rows : 5                                                                                     
*                                                                                                             
**                                                                                                            
***                                                                                                           
****                                                                                                          
*****                                                                                                         

10. WAP in C to display the pattern like a right angle triangle with a number

Pattern Example
1
12
123
1234
#include <stdio.h>
void main()
{
   int i,j,rows;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   for(i=1;i<=rows;i++)
   {
	for(j=1;j<=i;j++)
	   printf("%d",j);
	printf("\n");
   }
}

Output

Input number of rows : 5                                                                                     
1
12
123
1234
12345

11. WAP in C to make such a pattern like a right angle triangle with a number that will repeat a number in a row. 

Pattern Example
1
22
333
4444
55555
#include <stdio.h>
void main()
{
   int i,j,rows;

   printf("Input number of rows : ");
   scanf("%d",&rows);
   for(i=1;i<=rows;i++)
   {
	for(j=1;j<=i;j++)
	   printf("%d",i);
	printf("\n");
   }
}

Output

 

Input number of rows : 8                                                                                     
1
22
333
4444
55555
666666
7777777
88888888

12. WAP in C to make such a pattern like a right-angle triangle with a number increased by 1. 

The pattern like :

1
2 3
4 5 6
7 8 9 10
#include <stdio.h>
void main()
{
   int i,j,rows,k=1;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   for(i=1;i<=rows;i++)
   {
	for(j=1;j<=i;j++)
	   printf("%d ",k++);
	printf("\n");
   }
}

Output

Input number of rows : 4
1
2 3
4 5 6
7 8 9 10

13. WAP in C to make such a pattern like a pyramid with numbers increased by 1. 

       1 
    2  3 
  4  5  6 
7  8 9  10

Solution

#include <stdio.h>
void main()
{
   int i,j,k,t=1,sp,n;
   printf("Enter number of rows : ");
   scanf("%d",&n);
   sp=n+4-1;
   for(i=1;i<=n;i++)
   {
         for(k=sp;k>=1;k--)
            {
              printf(" ");
            }
	   for(j=1;j<=i;j++)
	   printf("%d ",t++);
	printf("\n");
    sp--;
   }
}

Output

 Enter number of rows: 4
      1 
    2  3 
  4  5  6 
7  8 9  10

14. WAP in C to make such a pattern like a pyramid with an asterisk. 

    * 
  * * 
 * * * 
* * * *

Solution

#include <stdio.h>
void main()
{
   int i,j,k,sp,n;
   printf("Enter number of rows : ");
   scanf("%d",&n);
   sp=n+4-1;
   for(i=1;i<=n;i++)
   {
         for(k=sp;k>=1;k--)
            {
              printf(" ");
            }
                      
	   for(j=1;j<=i;j++)
	   printf("* ");
	printf("\n");
    sp--;
   }
}

Output

Enter number of rows : 5
    * 
   * * 
  * * * 
 * * * *
* * * * *

15. WAP to calculate the factorial of a given number. 

Solution

#include <stdio.h>
void main(){
  int i,f=1,num;

  printf("Please enter the number : ");
  scanf("%d",&num);

  for(i=1;i<=num;i++)
      f=f*i;

  printf("Factorial of %d is: %d\n",num,f);
}

Output

Input the number : 5
The Factorial of 5 is: 120

16. WAP in C to make a pyramid pattern with a number repeating itself.

      1
   2   2
 3   3   3
4  4  4   4

Solution

#include <stdio.h>

void main()
{
   int i,j,k,sp,r,;
   printf("Please enter the number of rows : ");
   scanf("%d",&r);
   spc=r+4-1;
   for(i=1;i<=r;i++)
   {
         for(k=sp;k>=1;k--)
            {
              printf(" ");
            }
                      
	   for(j=1;j<=i;j++)
	   printf("%d ",i);
	printf("\n");
    sp--;
   }
}

Output

Please enter the number of rows : 5                                                                                      
        1                                                                                                     
       2 2                                                                                                    
      3 3 3                                                                                                   
     4 4 4 4                                                                                                  
    5 5 5 5 5

17. WAP in C to find the sum of the series [ 1-X^2/2!+X^4/4!- ………]. 

Example :

the sum = -0.415873

Number of terms = 5

value of x = 2.000000

Solution

#include <stdio.h>
void main()
{
	float x,sum,t,d;
	int i,n;
	printf("Please enter the Value of x :");
	scanf("%f",&x);
	printf("Please enter the number of terms : ");
	scanf("%d",&n);
	sum =1; t = 1;
	for (i=1;i<n;i++)
	{
	  d = (2*i)*(2*i-1);
	  t = -t*x*x/d;
	  sum =sum+ t;
	}
	printf("\nSum = %f\nNumber of terms = %d\nValue of x = %f\n",sum,n,x);
} 

Output

Please enter the Value of x :3
Please enter the number of terms : 6
Sum = -0.991049
Number of terms = 6
Value of x = 3.000000

18. WAP in C to display the n terms of harmonic series and their sum. 

Example

1 + 1/2 + 1/3 + 1/4 + 1/5 … 1/n terms

Solution

#include <stdio.h>
void main()
{
   int i,n;
   float s=0.0;
   printf("Please enter the number of terms : ");
   scanf("%d",&n);
   printf("\n\n");
   for(i=1;i<=n;i++)
   {
       if(i<n)
       {
     printf("1/%d + ",i);
     s+=1/(float)i;
       }
     if(i==n)
     {
     printf("1/%d ",i);
     s+=1/(float)i;
     }
     }
        printf("\nThe sum of Series upto %d terms : %f \n",n,s);
} 

Output

Please enter the number of terms : 10
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10 
The sum of Series upto 10 terms : 2.928968 

19. WAP in C to display the sum of the series [ 9 + 99 + 999 + 9999 …]. 

Solution

#include <stdio.h>
void main()
{ 
   long int n,i,t=9;
   int sum =0;
   printf("Please enter the number or terms :");
   scanf("%ld",&n);
   for (i=1;i<=n;i++)
  {
     sum +=t;
     printf("%ld ",t);
     t=t*10+9;
   }
   printf("\nThe sum of the series is %d \n",sum);
}

Output

Please enter the number or terms :10

9 99 999 9999 99999 999999 9999999 99999999 999999999 9999999999 

The sum of the series is -1773790788

20. WAP in C to print Floyd’s Triangle. 

Solution

#include <stdio.h>
void main()
{
    int i,j,n,p,q;
    printf("Please enter the number of rows : ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%2==0)
        {
            p=1;q=0;
            
        }
        else
        {
            p=0;q=1;
            
        }
        for(j=1;j<=i;j++)
        if(j%2==0)
        printf("%d",p);
        else
        printf("%d",q);
        printf("\n");
}
}

Output

Please enter the number of rows : 6
1
01
101
0101
10101
010101

Leave a Comment