Looping statements

Looping statements in C

Looping statement are the statements execute one or more statement again and again several number of times.
Lopping sattements are used when check same condition number of times.

Looping statements are as follows:
  1. while
  2. do while
  3. for

1.  while loop

syntax of while loop:
                    while( condition ) 
                    { 
                    statements to be executed	//Body of loop;
                    } 
                

in "while loop" check the condition if the condition is true then the body of the while loop is executed, and again check the condition. the while loop is executd until the condition gets false.

Flowchart for the while loop:
Sample program
1.Print the sum of the frist 10 numbers.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
         void main()
         {
           int n,sum;
           n=10;
           sum=0;
          while(n<=10)
           {
              sum=sum+n;
           }    

           printf("Sum of frist 10 natural numbers  is %d”, sum);
          getch();
        }
         
     

Output-

         sum of frist 10 natural numbers is 55 
          
2.Implement a program to find sum of digits of a number using while loop.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
         void main()
         {
           int n,sum;
           sum=0;
           printf("Enter the number\n");
           scanf("%d",&n;)
          while(n!=0)
           {
              rem=n%10; 
              sum=sum+rem;
              n=n/10;
           }    

           printf("Sum of digit of number is %d”, sum);
          getch();
        }
         
     

Output-

         Enter the number
         123
         Sum of digit of number is 6 
          

2. do while loop

syntax of do while loop:
                do 
                { 
                Statements	//Body of loop; 
                } while ( condition ); 
                

in "do while loop" frist time the body of loop get executed and then check the condition if condition is true the body of loop again executed. body of loop get executed until yhe condition get false. the difference between while and do while loop is in do while loop body of loop executed at least once even als condition is false.

Flowchart for the do while loop:
Sample program
1.Program to print table for the given number using do while loop
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
        void main()
        {    
           int i=1,n;    
           printf("Enter a number\n ");    
           scanf("%d",&n);   
           printf("The table of %d is\n"n); 
           do{    
           printf("%d \n",(n*i));    
           i++;    
           }while(i<=10);    
           getch();  
        }    
         
     

Output-

         Enter a number
         5
         The table of 5 is
         5
         10
         15
         20
         25
         30
         35
         40
         45
         50

        
2.Write a program to calculate the sum of digits in given number using do while loop.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
          void main()
           {
            int num,r,sum=0;
            clrscr();
            printf("enter any number");
            scanf("%d",&num);
            do{
            r=num%10;
            sum=sum+r;
            num=num/10;
              }while(num!=0);
            printf("sum of digits =%d",sum);
            getch();
          }
     
          }
         
     

Output-

         enter eny number
         123
         Sum of digits=6 
          

3.  for loop

syntax of for loop:
             for(initialization; condition; increment/decrement statement) 
             { 
                 Body of for loop; 
             } 
         

In "for loop" The initialization statement is executed only once.Then, the condition is evaluated. If condition is false (0), for loop is terminated. But if the condition is true (nonzero), codes inside the body of for loop is executed and then increment or decrement statement is executed and again condition is checked untill the condition is false body of for loop get executed. All expressions or statement in the for loop are optional. at the end of the for loop semicolon is there then for loop not executed.

Flowchart for the for loop:
Sample program
1.Print the frist 10 even numbers.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
         void main()
         {
           int i,n;
           printf("frist 10 even natural numbers are\n");
           for(i=1;i<21;i++)
            {  
               if(i%2==0)
               {
                printf("%i\n",i);
               }
            }    
           getch();
        }
         
     

Output-

         frist 10 even numbers are
         2
         4
         6
         8
         10
         12
         14
         16
         18
         20
          
2.Implement program to check the entered number is prime or not prime.
     
              
         #include<stdio.h> 
         #include<conio.h> 
     
         void main()
         {
           int n,i,k;
           k=0;
           printf("Enter the number for checking prime or not prime\n");
           scanf("%d",&n;)
           for(i=2;i<n/2;i++)
           {
              if(n%i==0)
              {
                  k=1;
              }
           } 
           if(k==0)
           {
               printf("The entered number is prime");
           }   
           else
           {
               printf("The entered number is not prime");
           }

           printf("Sum of digit of number is %d”, sum);
          getch();
        }
         
     

Output-

        Enter the number for checking prime or not prime
        37
        The entered number is prime 
          
Examples For Practice
  1. Write a c program to print "Hello" 10 times on console using do while loop.
  2. Write a c program to print sum of first 10 odd natural numbers using do while loop.
  3. Write a program to check number is Armstrong or not using for loop.
  4. Write a c program to reverse given number using while loop.