Java Program Of While Loops

01. Example:

public class whileLoopProgram {
  public static void main(String[] args) {
    
    int iteration = 1;
    
    while (iteration <= 6) {
      System.out.println(iteration);
      iteration++;
    }
    
  }
}

Output:

Table of Contents

1
2
3
4
5
6


02. Example:

import java.util.Scanner;

public class whileLoopProgram {
  public static void main(String[] args) {
      
    int iteration = 0, factorial = 1, N;
      
    Scanner input = new Scanner(System.in);
    System.out.print("Enter An Number: ");
    N = input.nextInt();
      
    while (iteration < N) {
      factorial  = factorial + factorial*iteration;
      iteration++;
    }
  
    System.out.println("nFactorial Is: "+factorial);
      
  }
}

Output:

Enter An Number: 5
Factorial Is: 120

Leave a comment