Posted by Somesh Shinde
On
Tuesday, 28 June 2016
Program For Factorial Numbers
Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1
prod | n | prod |
prod*n | |
1 | 5 | 5 |
5 | 4 | 20 |
20 | 3 | 60 |
60 | 2 | 120 |
120 | 1 | 120 |
Program | Output |
//Note: Scanner class work with JDK1.5 or above
import java.util.*;
class Factorial
{
public static void main(String args[])
{
int n, i, prod=1;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter a No.");
n=scan.nextInt();
for(i=n;i>=1;i--)
{
prod*=i; //prod=prod*i;
}
System.out.println("Factorial of " + n + " is " + prod);
}
}
| Please Enter a No.: 5
Factorial of 5 is 120
|
|
|
0 comments:
Post a Comment