Factorial of any number
Factorial of any number is the product of an integer and all the
integers below it for example factorial of 4 is 4! = 4 * 3 * 2 * 1 = 24.
Example
import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
int no, fect=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number
:");
no=s.nextInt();
for(int i=1; i<=no; i++)
{
fect=fect*i;
}
System.out.println("Factorial is :" +fect);
}
}
Output
Enter any
number :
6
Factorial is
:
720
Syntax to compile and run
java program
Syntax
for compile
-> c:/>javac Factorial.java
for run ->
c:/>java Factorial
Explnation of Code
·
Scanner
s=new Scanner(System.in): are used for receive input from keyboard.
·
nextInt(): method
are used for get integer type value from keyboard.
·
System.out.println("....."): are used
for display message on screen or console.
0 Comments