FACTORIAL IN JAVA

Factorials are the products , indicated by an exclamation mark .For instance  factorial 5 is written as 5! = 1*2*3*4*5 = 120

In general n! or (“enn factorial “) means the product of all the whole numbers from 1 to n ie 1*2*3…*n.

Program for Factorial

import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
int count ,i , fact=1;
System.out.println(“Enter any number whose factorial you want to find out :”);
Scanner sc =new Scanner(System.in);
count =sc.nextInt();          //Taking input from the user
if(count<0){
System.out.println(“Enter any number greater than 0”);
}
else{

for(i=1;i<=count;i++){
fact =fact*i;             //the loop runs till the count

}
System.out.println(“The factorial of “+count+” is : “+fact);
}
}
}

Leave a Reply