- This interview question is mainly asked to freshers in Java Interviews.
- It also represents an example of recursion.
- Factorial number means multiplication of all positive integer from one to that number.
- Two factorial: 2!= 2*1=2
- Three factorial: 3!= 3*2*1=6.
- Eight factorial: 8!= 8* 7*6*5*4*3*2*1=40320
where ' ! ' sign represents the factorial .
package com.javainhouse;
import java.util.Scanner;
public class FactiorialProgram {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to find factorial");
int n= in.nextInt();
int fact=1;
for (int i = 1; i < n; i++) {
fact=fact*i;
}
System.out.println("Factorial of "+n+" is "+fact);
}
}
|
Output:
Enter a number to find factorial
5
Factorial of 5 is 24
Post a Comment