PROGRAM FOR FACTORIAL OF A NUMBER

In general logic to find a factorial of a number is simple and easy.

The formula for finding the factorial of a number is : Factorial of a (n) number = n.(n-1).(n-2).(n-3). ...... .32.1.

If suppose we need to find factorial of number 5, it will be 5.4.3.2.1 = 120. In this hub I will be explaining you the code step by step that how a factorial of a number is calculated and printed on the console.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package pack;
import java.math.BigInteger;
import java.util.Scanner;
public class Fact
{
     public BigInteger fact(int m)
     {
        BigInteger sum = BigInteger.ONE;
        for(int i=1;i<=m;i++)
        sum=sum.multiply(BigInteger.valueOf(i));
        return sum;
     }
     public static void main(String args[])
    { 
        System.out.print("Enter any Number = ");
        Scanner x=new Scanner(System.in);
        int n=x.nextInt();
        Fee f=new Fee();
        BigInteger b=f.fact(n);
        System.out.println("the fact is " +b);
    }
}

Output :Factorial Number
IPPal