factorial program in java

/*Program to calculate factorial of given number*/

factorial formula n!=1 * 2 * 3 * 4 ..........(n-1) * n

for example

factorial of  1! = 1

factorial of  2! = 1—2 = 2

factorial of  3! = 1—2—3 = 6

factorial of  4! = 1—2—3—4 = 24

factorial of  5! = 1—2—3—4—5 = 120

import java.util.*;

class P2
{
public static void main(String args[])
{
int i , number , factorial ;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Number");

number = sc.nextInt();   //input factorial number

for (i=1,factorial=1 ; i<=number ; i++)
factorial = factorial * i ;

System.out.println("The factorial of "+number+" : "+factorial);
}
}

Output of factorial program in java :

Enter Number : 7

The factorial of 7 : 5040

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *