CODE
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);
}
}
Factorial of number 6 is 720
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
5
using for
.9
using do-while
.