Factorial Program in Java using While loop iterative method

In this tutorial we will learn writing Java program to calculate Factorial of given number using while loop. Here basically we will be using iterative approach which is different from recursive approach.

Please check this Program

Factorial Program in java using recursive method.

Factorial Program in Java using for loop iterative method.

Factorial Program in java using iterative method while loop

import java.util.*;
public class Main
{
    static long fact(long num){
    long i=1,fact=1;
        while(i<=num){
            fact*=i;
            i++;
        }
        return fact;
    }
    public static void main(String[] args) {
        long num;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a whole number to find Factorial ");
        num= sc.nextInt();
        System.out.println("Factorial = "+fact(num));
    }
}

Output

Enter a whole number to find Factorial 
4
Factorial = 24