Loop Control Structures – Theory and Programs [Learn to code]

What are loop control structures? Learn syntax for Loop Control Structures, types of loops and different programs on Loop Control Structures.

  • 025. Write a Program to print numbers 1 to n using while loop in C language
    #include<stdio.h>
    #include<conio.h> 
    main()
    {
      int i=1,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      while(i<=n)
      {
    	   printf("%dt",i);
    	   i++;
      }
      getch();
    }
  • 026. Write a Program to print numbers n to 1 using Do While loop in C language
    #include<stdio.h>
    #include<conio.h> 
    main()
    {
      int i=1,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      i=n;
      do
      {
    	   printf("%dt",i);
    	   i--;
      }while(i>=1);
      getch();
    }
  • 027. Write a Program to print first n even numbers in C language
    #include<stdio.h>
    #include<conio.h> 
    main()
    {
      int i=2,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      while(i<=n)
      {
    	   printf("%dt",i);
    	   i=i+2;
      }
      getch();
    }
  • 028. Write a Program to print first n odd numbers in C language
    #include<stdio.h>
    #include<conio.h> 
    main()
    {
      int i=1,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      while(i<=n)
      {
    	   printf("%dt",i);
    	   i=i+2;
      }
      getch();
    }
  • 029. Write a Program to accept a number and print the number in reverse order. E.g. if 1324 is the number then the output will be 4231 in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
      int rem,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      while(n>0)
      {
    	   rem=n%10;
    	   printf("%d",rem);
    	   n=n/10;
      }
      getch();
    }
  • 030. Write a Program to accept a number and print sum of it’s digits in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
      int rem,sum=0,n
      clrscr();
      printf("Enter n : ");
      scanf("%d",&n);
      while(n>0)
      {
    	   rem=n%10;
    	   sum=sum+rem;
    	   n=n/10;
      }
      printf("Sum of digits....%d",sum);
      getch();
    }
  • 031. Write a program to accept a number from user and check it it is Armstrong number or not i.e. 153 = 1^3 + 5^3 + 3^3 = 153 in C language
    #include
    #include
    main()
    {
      int temp,rem,sum=0,n;
      clrscr();
      printf("Enter n : ");
      scanf("%d", &n);
      temp=n;
      while(n > 0)
      {
    	   rem=n%10;
    	   sum=sum+rem*rem*rem;
    	   n=n/10;
      }
      if(temp==sum)
    		printf("Armstrong Number");
      else
    		printf("Not an Armstrong Number");
      getch();
    }
  • 032. Write a program to print table of a given number n in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    { 
     int i,n;
     clrscr();
     printf("Enter number : ");
     scanf("%d",&n);
     for(i=1;i<=10;i++)
    	printf("%d X %d = %dn",n,i,n*i);
     getch();
    }
  • 033. Write a program to print sum of given first n numbers in C language
    #include<stdio.h> 
    #include<conio.h>
    main()
    {
     int n,newn,i,sum=0;
     clrscr();
     printf("Enter number : ");
     scanf("%d",&n);
     printf("Numbers entered....n");
     for(i=1;i<=n;i++)
     {
    	scanf("%d",&newn);
    	sum=sum+newn;
     }
     printf("Sum of given n digits is... %d",sum);
    }
  • 034. Write a program to print following outputs in C language
    * * * * * *
    * * * * * *
    * * * * * *
    * * * * * *
    * * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=n;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 035. Write a program to print following outputs in C language
    *
    * *
    * * *
    * * * *
    * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 036. Write a program to print following outputs in C language
    * * * * *
    * * * *
    * * *
    * *
    *
    
    #include<stdio.h> 
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 037. Write a program to print following outputs in C language
    * * * * *
      * * * *
        * * *
          * *
            *
    
    #include<stdio.h> 
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<i;k++)
    			{
    			 printf(" ");
    			}
    		for(j=i;j<=n;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 038. Write a program to print following outputs in C language
            *
          * *
        * * *
      * * * *
    * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		printf("n");
    	}
    	getch();
    }
  • 039. Write a program to print following outputs in C language
            *
          * * *
        * * * * *
      * * * * * * *
    * * * * * * * * *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    	}
    	getch();
    }
  • 040. Write a program to print following outputs in C language
    * * * * * * * * *
      * * * * * * *
        * * * * *
          * * *   
            *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=n;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    	}
     
    		printf("n");
    	}
    	getch();
    }
  • 041. Write a program to print following outputs in C language
            *
          * * *
        * * * * *
      * * * * * * *
    * * * * * * * * *
      * * * * * * *
        * * * * *
          * * *   
            *
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
     
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    	}
     
    	for(i=n-1;i>=1;i--)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("*");
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("*");
    			}
     
    		printf("n");
    }
    	getch();
    }
  • 042. Write a program to print following outputs in C language
    1
    12
    123
    1234
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			printf("%d",j);			
    		printf("n");
    	}
    	getch();
    }
  • 043. Write a program to print following outputs in C language
    1
    22
    333
    4444
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    			printf("%d",i);
    		printf("n");
    	}
    	getch();
    }
  • 044. Write a program to print following outputs in C language
       A
      BBB
     CCCCC
    DDDDDDD
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,k,n;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	//ASCII Code for 'A' is 65
    	for(i=1;i<=n;i++)
    	{
    		for(k=1;k<=(n-i);k++)
    			{
    			 printf(" ");
    			}
    		for(j=1;j<=i;j++)
    			{
    			 printf("%c",64+i);
    			}
    		for(j=2;j<=i;j++)
    			{
    			 printf("%c",64+i);
    			}
     
    		printf("n");
    	}
    	getch();
    }
  • 045. Write a program to print following outputs in C language
    ABCDEEDCBA
    ABCD  DCBA
    ABC    CBA
    AB      BA
    A        A
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
      int i,n,j,k;
      clrscr();
      printf("Enter number........");
      scanf("%d",&n);
      for(i=n-1;i>=0;i--)
      {
    		for(j=0;j<=i;j++)
    			printf("%c",65+j);
    		for(k=1;k<(n-i);k++)
    			printf("  ");
    		for(j=i;j>=0;j--)
    			printf("%c",65+j);
    	printf("n");
      }
      getch();
    }
  • 046. Write a program to print following outputs in C language
    1
    11
    121
    1231
    12341
    123451
    
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=0;i<=n;i++)
    	{
    		for(j=1;j<=i;j++)
    		{
    			printf("%d",j);
    		}
    		printf("1n");
    	}
    	getch();
    }
  • 047. Write a program to accept a number from user and print it’s factorial in C language

    Eg: factorial of 5 is:-
    5! = 5 * 4 * 3 * 2 * 1=120

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,fact=1,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    		fact=fact*i;
    	printf("facttorialof the given number is...%d",fact);
    	getch();
     
    }
  • 048. Write a program to accept a number from user and print if it is prime or not in C language
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(i=2;i<=n/2;i++)
    	{
    		if(n%i==0)
    		{
    		printf("Not Prime");
    		getch();
    		exit(0);
    		}
    	}
    	printf("Prime ");
    	getch();
    }
  • 049. Write a program to accept a number and print prime numbers between 2 and n in C language
     
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,flag=1,n,newn;
    	clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	for(newn=2;newn<=n;newn++)
    	{
    		flag=1;
    		for(i=2;i<=newn/2 ;i++)
    		{
    			if(newn%i==0)
    			{
    			flag=0;
    			break;
    			}
    		}
    		if(flag==1)
    			printf("%d ",newn);
    	}
    	getch();
    }
  • 050. Write a program to accept a number and print fibonacci series upto that level in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int pre=1,cur=1,temp,i,n;
            clrscr();
    	printf("Enter number...");
    	scanf("%d",&n);
    	printf("%dt%d",pre,cur);
    	for(i=3;i<=n;i++)
    		{
    		 temp=cur;
    		 cur=pre+cur;
    		 pre=temp;
    		 printf("t%d",cur);
    		}
    	getch();
    }
  • 051. Write a program to print digits, alphabets in capital and lower case in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	clrscr();
    	for(i=65;i<=90;i++)
    		  printf("%c  ",i);
     
    	printf("nnnn");
    	for(i=97;i<=122;i++)
    		  printf("%c  ",i);
     
    	printf("nnnn");
    	for(i=48;i<=57;i++)
    		  printf("%ct",i);
     
    	getch();
    }
  • 052. Write a program to accept a number n from user and Add n terms of the series in C language

    1/2! + 2/3! + 3/4! + 4/5! + 5/6! + ………

     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,n;
    	float sum=0,fact=1;
    	clrscr();
    	printf("Enter number....");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
    	{
    		fact=1;
    		for(j=1;j<=i+1;j++)
    			fact=fact*j;
    	sum=sum+i/fact;
    	}
    	printf("Sum of the series....%f",sum);
    	getch();
    }
  • 053. Write a program to read n numbers (Xi) from the user and print out their average and standard deviation. Formulae are

    Average = (Sum of Xi)/n

     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,newn,sum_avg=0;
    	float avg;
    	clrscr();
    	printf("Enter number : ");
    	scanf("%d",&n);
    	printf("Enter numbers...");
    	for(i=1;i<=n;i++)
    	{
    		scanf("%d",&newn);
    		sum_avg=sum_avg+newn;
    	}
    	avg=sum_avg/(float)n; //Type casting.
     
    	printf("nAverage = %f",avg);
    	getch();
    }
  • 054. Write a program to print out ASCII chart on a single screen (all 256 characters from 0 to 255) in a tabular form. The ASCII code should be followed by the corresponding character in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
     int i;
     clrscr();
     for(i=0;i<=255;i++)
    	printf("%d-%ct",i,i);
     getch();
    }
  • 055. Write a program to print following output
    ******************************* 
    *                             *
    *                             *  
    *                             *
    *******************************
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j;
    	clrscr();
    	for(i=1;i<=80;i++)
    		printf("*");
    	for(i=2;i<=23;i++)
    	{
    		printf("*");
    		for(j=2;j<=79;j++)
    			printf(" ");
    		printf("*");
    	}
    	for(i=1;i<=80;i++)
    		printf("*");
     
    	getch();
     
    }
  • 056. Write a program to print following output

    #include<stdio.h>
    #include<conio.h>
    main()
    {
            int i, j;
            clrscr();
            printf("╔");
            for(i=2;i<=79;i++)
                    printf("═");
            printf("╗");
            for(i=2;i<=24;i++)
            {
                    printf("║");
                    for(j=2;j<=79;j++)
                            printf(" ");
                    printf("║");
            }
            printf("╚");
            for(i=2;i<=79;i++)
                    printf("═");
            printf("╝");
     
            getch();
    }
  • 057. Write a program to accept a single value interger from user and print that integer in words in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n;
    	clrscr();
    	printf("Enter number...");
    	scanf("%1d",&n);  //%1d to scan single integer
    	switch(n)
    	{
    		case 0 : printf("Zero");break;
    		case 1 : printf("One");break;
    		case 2 : printf("Two");break;
    		case 3 : printf("Three");break;
    		case 4 : printf("Four");break;
    		case 5 : printf("Five");break;
    		case 6 : printf("Six");break;
    		case 7 : printf("Seven ");break;
    		case 8 : printf("Eigth ");break;
    		case 9 : printf("Nine ");break;
    	}
    	getch();
    }

057. Write a program to accept a single value interger from user and print that integer in words in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int n;
	clrscr();
	printf("Enter number...");
	scanf("%1d",&n);  //%1d to scan single integer
	switch(n)
	{
		case 0 : printf("Zero");break;
		case 1 : printf("One");break;
		case 2 : printf("Two");break;
		case 3 : printf("Three");break;
		case 4 : printf("Four");break;
		case 5 : printf("Five");break;
		case 6 : printf("Six");break;
		case 7 : printf("Seven ");break;
		case 8 : printf("Eigth ");break;
		case 9 : printf("Nine ");break;
	}
	getch();
}

056. Write a program to print following output

#include<stdio.h>
#include<conio.h>
main()
{
        int i, j;
        clrscr();
        printf("╔");
        for(i=2;i<=79;i++)
                printf("═");
        printf("╗");
        for(i=2;i<=24;i++)
        {
                printf("║");
                for(j=2;j<=79;j++)
                        printf(" ");
                printf("║");
        }
        printf("╚");
        for(i=2;i<=79;i++)
                printf("═");
        printf("╝");
 
        getch();
}

055. Write a program to print following output

******************************* 
*                             *
*                             *  
*                             *
*******************************
#include<stdio.h>
#include<conio.h>
main()
{
	int i,j;
	clrscr();
	for(i=1;i<=80;i++)
		printf("*");
	for(i=2;i<=23;i++)
	{
		printf("*");
		for(j=2;j<=79;j++)
			printf(" ");
		printf("*");
	}
	for(i=1;i<=80;i++)
		printf("*");
 
	getch();
 
}

054. Write a program to print out ASCII chart on a single screen (all 256 characters from 0 to 255) in a tabular form. The ASCII code should be followed by the corresponding character in C language

 
#include<stdio.h>
#include<conio.h>
main()
{
 int i;
 clrscr();
 for(i=0;i<=255;i++)
	printf("%d-%c\t",i,i);
 getch();
}

053. Write a program to read n numbers (Xi) from the user and print out their average and standard deviation. Formulae are

Average = (Sum of Xi)/n

 
#include<stdio.h>
#include<conio.h>
main()
{
	int n,i,newn,sum_avg=0;
	float avg;
	clrscr();
	printf("Enter number : ");
	scanf("%d",&n);
	printf("Enter numbers...");
	for(i=1;i<=n;i++)
	{
		scanf("%d",&newn);
		sum_avg=sum_avg+newn;
	}
	avg=sum_avg/(float)n; //Type casting.
 
	printf("\nAverage = %f",avg);
	getch();
}

052. Write a program to accept a number n from user and Add n terms of the series in C language

1/2! + 2/3! + 3/4! + 4/5! + 5/6! + ………

 
#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,n;
	float sum=0,fact=1;
	clrscr();
	printf("Enter number....");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		fact=1;
		for(j=1;j<=i+1;j++)
			fact=fact*j;
	sum=sum+i/fact;
	}
	printf("Sum of the series....%f",sum);
	getch();
}

051. Write a program to print digits, alphabets in capital and lower case in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i;
	clrscr();
	for(i=65;i<=90;i++)
		  printf("%c  ",i);
 
	printf("\n\n\n\n");
	for(i=97;i<=122;i++)
		  printf("%c  ",i);
 
	printf("\n\n\n\n");
	for(i=48;i<=57;i++)
		  printf("%c\t",i);
 
	getch();
}

050. Write a program to accept a number and print fibonacci series upto that level in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int pre=1,cur=1,temp,i,n;
        clrscr();
	printf("Enter number...");
	scanf("%d",&n);
	printf("%d\t%d",pre,cur);
	for(i=3;i<=n;i++)
		{
		 temp=cur;
		 cur=pre+cur;
		 pre=temp;
		 printf("\t%d",cur);
		}
	getch();
}

049. Write a program to accept a number and print prime numbers between 2 and n in C language

 
#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
	int i,flag=1,n,newn;
	clrscr();
	printf("Enter number...");
	scanf("%d",&n);
	for(newn=2;newn<=n;newn++)
	{
		flag=1;
		for(i=2;i<=newn/2 ;i++)
		{
			if(newn%i==0)
			{
			flag=0;
			break;
			}
		}
		if(flag==1)
			printf("%d ",newn);
	}
	getch();
}