Find Factorial of a Number in Java

In this program, you'll learn to find the factorial of a number using for and while loop in Java. The for statement is similar to the while statement, but it is often easier to use if you are counting or indexing because it combines three elements of many loops:  initialization, testing, and Increment.

Initialization: It allows the variable to be initialize. Such as: int i = 0; int j = 2;
Termination (or condition): It allows to check the certain condition. If condition is true then all statements and processes written in the for block will be executed otherwise ignored. Condition such as: i <= 5; i <j;
Increment: It allows the how much increase the given variable. Such as: i++; j++;

General Form :

The for and equivalent while statements have these forms : 
for (initial-statement; condition; next-statement) {

The factorial of a positive number n is given by:

Factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n

Example : Printing a table of factorial of 10

When you run the program, the output will be:

10! is : 1

Previous Post Next Post