Java Program For Factorial
Chapter:
Interview Programs
Last Updated:
14-07-2016 08:05:56 UTC
Program:
/* ............... START ............... */
import java.util.Scanner;
public class JavaFactorialExample {
public static void main(String args[]) {
int i, factorial = 1, n;
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number for Factorial");
n = input.nextInt();
for (int j = 1; j <= n; j++) {
factorial = factorial * j;
}
System.out.println(factorial);
}
}
/* ............... END ............... */
Output
Please enter a number for Factorial
8
40320
Notes:
-
Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.
- Eg : 5! = 5*4*3*2*1 = 120
Tags
Factorial, Java