Menu
Topics Index
...
`


Control Statements > Iteration statements (Loops) >
Siva Nookala - 23 Feb 2017
Factorial is the process multiplying all the natural numbers below the given number. This is very useful in probability for calculating the permutations and combinations. Here we will talk about the "Factorial Using While Loop" Java program. This Java program shows how to calculate the factorial of a given number using while Loop In Java.

Factorial using while loop
class FactorialUsingWhile
{
    public static void main(String arg[])
    {
        int factorial = 1;
        int number = 6;
        
        int i = 1;
        while(i <= number)
        {
            factorial *= i;
            i++;
        }
        
        System.out.println("Factorial of number " + number + " is " + factorial);
    
    }
}
OUTPUT

Factorial of number 6 is 720

DESCRIPTION

This program finds the factorial of the given number 6, by multiplying the variable factorial with various values of i - 1, 2, 3, 4, 5, 6. So at the end of the while loop the value of factorial will be 1 * 2 * 3 * 4 * 5 * 6 = 720

THINGS TO TRY
  • Try to find the factorial of 5 using for.
  • Try to find the factorial of 9 using do-while.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.