JAVA programs

Java Program to find factorial using while loop

In this program we going to find factorial of first 10 natural no. using the while loop. loops are used to perform particular task repeatedly.

 Factorial of a number n is denoted as n! and we can find factorial of given no using n! is: 1 * 2 * 3 * …….. (n-1) * n.

 We find factorial using logic shown in below program.

Program

public class WhileDemo

{

    public static void main(String[] args)

    {

        int factorial=1;

        int count=1;

       

        while(count<=10)

        {

            System.out.println("factorial of "+count+" is :");

            System.out.println(factorial*=count);

            count++;

        }

    }

}

When Run The Program Following Output:

factorial of 1 is :

1

factorial of 2 is :

2

factorial of 3 is :

6

factorial of 4 is :

24

factorial of 5 is :

120

factorial of 6 is :

720

factorial of 7 is :

5040

factorial of 8 is :

40320

factorial of 9 is :

362880

factorial of 10 is :

3628800

Description

The while loop repeat (iterate) a block of code as long as a specified condition is true

Syntax as below:

while(condition)

{
 
// statement block to be executed if condition is true
}

 

When we want to repeat the code we use the loops in java we can use loop like for, while, do-while, for-each.

Releted Programs

© 2017 codewmAll rights reserved.