While-loop-in-C .

In this session i have discussed while loop in C .


The while loop is very fast loop in C .

While Loop Syntax :

    while(test condition)
    {
        loop body
    }
    

While loop is a entry control loop . In this loop first check the condition if condition is true then the loop body will be executed . After execution of the loop body control once again check if condition if it is true then control again come inside the loop body this process continue .. until the condition become false .


Example : Write a program that print the number from 1-10 using while loop .

    #includ<stdio.h> 
    int main()
    {
            int i=1;
            while(i<=10)
            {
                printf("%d",i);
                printf(" ");
                i++;
            }
            return 0;
    }    
    OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a program that print the table of 2 using while loop .

    #includ<stdio.h> 
    int main()
    {
            int i=1;
            int t;
            while(i<=10)
            {
                t=i*2;
                printf("%d",t);
                printf(" ");
                i++;
            }
            return 0;
    }    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

Example : Write a program that print the even number using while loop .

    #includ<stdio.h> 
    int main()
    {
          int i=1;
          while(i<=10)
          {
              if(i%2==0)
              {
                  printf("%d",i);
                  printf(" ");
              }
                i++;
           }
    }    
    OUTPUT : 2 4 6 8 10
    

Example : Write a program that print the odd number using while loop .

    #includ<stdio.h> 
    int main()
    {  
          int i=1;
          while(i<=10)
          {
               if(i%2!=0)
               {
                   printf("%d",i);
                   printf(" ");
               }
                i++;
          }
          return 0;        
    }    
    OUTPUT : 1 3 5 7 9
    

Example : Write a program that print the reverse number of any given number using while loop . 123=321

    #includ<stdio.h> 
    int main()
    {  
          int n = 123; //reverse the number 321
          int rem ,rev=0;
          while (n>0)
          {
               rem = n % 10;
               rev = (rev *10)+rem;
               n = n / 10;
          }
          printf("%d",rev);
          return 0;
    }    
    OUTPUT : 321
    

Example : Write a program that sum the individual digits of any given number using while loop . 123=6

    #includ<stdio.h> 
    int main()
    {
            int n = 123; 
            int rem;
            int sum=0;
            while (n>0)
            {
                rem = n % 10;
                sum=sum+rem;
                n = n / 10;
            }
            printf("%d",sum);
            return 0;
    }    
    OUTPUT : 6
    


Example : Write a program that print the palindrome number using while loop . 121=121

    #includ<stdio.h> 
    int main()
    {
            int n = 121;
            int k = n;
            int rem ;
            int rev=0 ;

            while (n>0)
            {
                rem = n % 10;
                rev = (rev*10)+rem;
                n = n / 10;
            }
            if (k == rev)
            {
                printf("Number is palindrome");
            }
            else
            {
                printf("Number is not palindrome");
            }
            return 0;
    }    
    OUTPUT : Number is palindrome
    


Example : Write a program that print the armstrong number using while loop . 153=153 means 1*1*1+5*5*5+3*3*3 .

    #includ<stdio.h> 
    int main()
    {
            int n = 153;
            int k = n;
            int rem ;
            int sum=0 ;

            while (n>0)
            {
                rem = n % 10;
               sum = sum+(rem*rem*rem);
                n = n / 10;
            }
            if (k == sum)
            {
                printf("Number is armstrong");
            }
            else
            {
                printf("Number is not armstrong");
            }
            return 0;
    }    
    OUTPUT : Number is armstrong
    


Example : Write a program that print the fibonacci series using while loop . i.e 0 1 1 2 3 5 8 .

    #includ<stdio.h> 
    int main()
    {
            int sum=0;
            int f0 = 0;
            int f1 = 1;
            int i = 1;
            printf("%d",f0);
            printf(" ");
            printf("%d",f1);
            printf(" ");
            while (i<6)
            {
                sum = f0 + f1;
                f0 = f1;
                f1 = sum;
                i++;
                printf("%d",sum);
                printf(" ");
            }            
            return 0;
     }   
    
    0 1 1 2 3 5 8
    

Example : Write a program that print the table 01-10 using while loop .

    #includ<stdio.h> 
    int main()
    {       
            int i = 1;
            int j ;
            int t = 0;
            while (i <= 10)
            {
              j = 1;
              while(j<=10)
              {
                 t = i * j;
                 if(t<10)
                 {
                    printf("0%d",t);
                    printf(" ");
                 }
                 else
                 { 
                    printf("%d",t);
                    printf(" ");
                 }
                  j++;
              }
              i++;
              printf("\n");
           }            
           return 0;
    }
    

    OUTPUT :  01 02 03 04 05 06 07 08 09 10
              02 04 06 08 10 12 14 16 18 20
              03 06 09 12 15 18 21 24 27 30
              04 08 12 16 20 24 28 32 36 40
              05 10 15 20 25 30 35 40 45 50
              06 12 18 24 30 36 42 48 54 60
              07 14 21 28 35 42 49 56 63 70
              08 16 24 32 40 48 56 64 72 80
              09 18 27 36 45 54 63 72 81 90
              10 20 30 40 45 60 70 80 90 100
    

Welcome to AndroidATPoint.

back image                     next image