While-loop-in-Java .

In this session i have discussed while loop in Java .


The while loop is very fast loop in Java .

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 java program that print the number from 1-10 using while loop .

  class Hello{ 
         
      public static void main(String args[]){  
  
            int i=1;
            while(i<=10){
            
                System.out.print(i);
                System.out.pirnt(" ");
                i++;
            }
       }    
  }    
    OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

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

 class Hello{ 
         
      public static void main(String args[]){  
  
          int i=1;
          int t;
          while(i<=10){
            
              t=i*2;
              System.out.print(t);
              System.out.print(" ");
              i++;
          }
      }
  }    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

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

 class Hello{ 
         
      public static void main(String args[]){  
 
        int i=1;
         while(i<=10){
          
             if(i%2==0){
              
                 System.out.print(i);
                 System.out.print(" ");
              }
            i++;
         }
  }    
    OUTPUT : 2 4 6 8 10
    

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

 class Hello{ 
         
      public static void main(String args[]){  
          int i=1;
          while(i<=10){
          
               if((i%2)!=0){
               
                   System.out.print(i);
                   System.out.print(" ");
               }
            i++;
          }
       }        
   }    
    OUTPUT : 1 3 5 7 9
    

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

 class Hello{ 
         
      public static void main(String args[]){  

          int n = 123; //reverse the number 321
          int rem ,rev=0;
          while (n>0){
          
               rem = n % 10;
               rev = (rev *10)+rem;
               n = n / 10;
          }
          System.out.println(rev);
      }
 } 
    OUTPUT : 321
    

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

 class Hello{ 
         
      public static void main(String args[]){  
 
            int n = 123; 
            int rem;
            int sum=0;
            while (n>0){
            
                rem = n % 10;
                sum=sum+rem;
                n = n / 10;
            }
            System.out.println(sum);
       }
  }    
    OUTPUT : 6
    


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

 class Hello{ 
         
      public static void main(String args[]){  
 
            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){
            
                System.out.println("Number is palindrome");
            }
            else{
            
                System.out.println("Number is not palindrome");
            }
      }
 }         
    OUTPUT : Number is palindrome
    


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

 class Hello{ 
         
      public static void main(String args[]){  
 
            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){
            
                System.out.println("Number is armstrong");
            }
            else
            {
                System.out.println("Number is not armstrong");
            }
      } 
 }    
    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 .

 class Hello{ 
         
      public static void main(String args[]){  
 
            int sum=0;
            int f0 = 0;
            int f1 = 1;
            int i = 1;
            System.out.print(f0);
            System.out.print(" ");
            System.out.print(f1);
            System.out.print(" ");
            while (i<6){
            
                sum = f0 + f1;
                f0 = f1;
                f1 = sum;
                i++;
                System.out.print(sum);
                System.out.print(" ");
            }            
           
      }   
 }   
    0 1 1 2 3 5 8
    

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

 class Hello{ 
         
      public static void main(String args[]){  
 
            int i = 1;
            int j ;
            int t = 0;
            while (i <= 10){
            
              j = 1;
              while(j<=10){
              
                 t = i * j;
                 if(t<10){
                 
                   System.out.print("0"+t);
                   System.out.print(" ");
                 }
                 else{
                  
                    System.out.print(t);
                    System.out.print(" ");
                 }
                  j++;
              }
              i++;
              System.out.print("\n");
           }
      }            
 }
    

    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