Create a Java program that prompts the user for an integer 'n' and prints the factorial of 'n'. Your program should consist of a main method where all your logic resides. Assume the input integer 'n' is non-negative. The main method should read from the standard input and print to the standard output. For example, if the input is 5, the output should be 120 since 1*2*3*4*5 = 120.
Method "main"
Calculating factorial of a given number
Report a typo
Sample Input 1:
5Sample Output 1:
120Sample Input 2:
0Sample Output 2:
1Write a program in Java 17
import java.util.Scanner; // 3.1 Import necessary library
public class Main { // 3.6 Always use 'Main' as class name
// 3.0 No method main code here, user write their own
public static void main(String[] args) {
// 3.2 Create a Scanner object
Scanner scanner = new Scanner(System.in);
// TODO: 1. Ask user for an integer 'n' using scanner.nextInt()
// TODO: 2. Print the factorial of 'n'
scanner.close(); // Always close the scanner when done
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.