Assignment: Module 03- Part 03
Write a class FactorialExample to accept the number and find the factorial of it.
Input:
Enter the number 6
Output:
The sum of n no is=21
Here's a Java code for finding the factorial of a number:
import java.util.Scanner;
class FactorialExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = sc.nextInt();
int result = 1;
for (int i = 1; i <= num; i++) {
result = result * i;
}
System.out.println("The factorial of " + num + " is " + result);
}
}
Example output when user enters 6:
Enter the number: 6
The factorial of 6 is 720