Factorial Number
Program to calculate factorial of a number
Factorial of a positive integer is the multiplication of all positive integers greater than 0 and smaller than or equal to the number. Factorial of a number is denoted by (!) sign.
Example : 5! = 5*4*3*2*1 = 120
6! = 6*4*3*2*1 = 720
Program :-
import java.io.*;
import java.util.*;
class Factorial
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,i,p=1;
System.out.println("Enter Number");
n= in.nextInt(); //Input number from user
for(i=1;i<=n;i++) //Loop from 1 to the entered number
{
p=p*i; // calculating factorial
}
System.out.println("Factorial of "+n+" is: "+p);//Printing result
} // end of main method
} // end of class
Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.
All the Best :)
Keep Learning :)
Komentarze