Factorial of a number Enter by user in Java.
EXAMPLE :-
import java.util.Scanner;
public class fact {
public static void main(String[] args)
{
int num;
System.out.println("Enter the number: ");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
scanner.close();
long factorial = 1;
int i = 1;
while(i<=num)
{
factorial = factorial * i;
i++;
}
System.out.println("Factorial of "+num+" is: "+factorial);
}
}
OUTPUT
Enter the number:
5
Factorial of 5 is: 120
No comments:
Post a Comment
Please do not any spam link in Comment Box