How to find a Factorial Number.

Factorial Number

Factorial Number is a number which is calculated by multiplying the given number from its all previous number till 1.
Example-: Factorial of number 5 = 5*4*3*2*1=120.

Java code for finding factorial-:

import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print(“Enter a number to print its factorial=”);
int fact=1,number=0;
number=s.nextInt();
for(int i=number;i>=1;i–)
{
fact=i*fact;
System.out.print(i);
if(i!=1)
System.out.print(“*”);
}
System.out.print(” = “+fact);
System.out.println(“\nThe factorial of “+number+” is “+fact);
}
}

Output-:

Leave a Reply