Factorial Number Using Java


Factorial means multiplying the given number by decreasingly value by 1.


Ex: 5!
        5*4*3*2*1=120
         so 5!=120.


Example:


import java.util.Scanner;


public class Factorial {


public static void main(String[] args) {

int fact=1;
System.out.println("Enter number to factorial:");

Scanner sc=new Scanner(System.in);
int input=sc.nextInt();

for(int i=input; i>=1; i--)
{
fact=fact*i;

}
System.out.println("Factorial of " +input + " is: " +fact);
}

}

Output:

Enter number to factorial:
5
Factorial of 5 is: 120





No comments:

Post a Comment