Java – While Loop

While loop: Execute a block of instructions repeatedly until the condition is false. We use while loop only when don ’t know the number of iterations to do.

Syntax:

    while(condition)
    {
                statements;
    }

    FlowChart:

    Program to Display Last Digit of given number

    • When we divide any number with 10 then last digit will be the remainder.
    • 1234%10 -> 4
    • 1005%10 -> 5
    • 200%10 -> 0
      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              int d = n%10;
                              System.out.println(“Last digit is : ” + d);
                  }
      }

      Program to remove the last digit of number:

      • When we divide any number with 10 then it gives quotient by removing last digit
      • 1234/10 -> 123
      • 1000/10 -> 100
      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              n = n/10;
                              System.out.println(“After removing last digit : ” + n);
                  }
      }

      Display digits of a number in reverse order:

      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              while(n!=0)
                              {
                                          System.out.println(n%10);
                                          n=n/10;
                              }
                  }
      }

      Program to Count the digits in given number

      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              int count=0;
                              while(n!=0)
                              {
                                          n=n/10;
                                          count++;
                              }
                              System.out.println(“Digits count : ” + count);
                  }
      }

      Display sum of the digits in the given number

      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              int sum=0;
                              while(n!=0)
                              {
                                          sum = sum +  n%10;
                                          n=n/10;
                              }
                              System.out.println(“Sum of digits : ” + sum);
                  }
      }

      Program to display the sum of even digits:

      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              int sum=0, r;
                              while(n>0)
                              {
                                          r = n%10;
                                          if(r%2==0)
                                          {
                                                      sum = sum+r;
                                          }
                                          n = n/10;
                              }
                              System.out.println(“Sum of Even digits are : ” + sum);
                  }
      }

      Program to count the number of 0 digits in the given number:

      import java.util.Scanner;
      class Code
      {
                  public static void main(String[] args)
                  {
                              Scanner scan = new Scanner(System.in);
                              System.out.print(“Enter Num : “);
                              int n = scan.nextInt();
                              int count=0, r;
                              while(n>0)
                              {
                                          r = n%10;
                                          if(r==0)
                                          {
                                                      count++;
                                          }
                                          n = n/10;
                              }
                              System.out.println(“Zero’s count : ” + count);
                  }
      }
      Scroll to Top