Factorial Finder

Report a typo

You have a Java code designed to calculate the factorial of a given number n. However, it's important to ensure that any other developer reading this code can easily understand its functionality. Your task is to thoroughly comprehend the code and provide appropriate comments.

Fill in the gaps with the relevant elements
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number: ");
        int n = sc.nextInt(); 
        int result = 1; 

        
        for (int i = 1; i < n + 1; i++) {
            result *= i;
        }

        System.out.println("The factorial of " + n + " is: " + result); 

        sc.close();
    }
}
// Calculate the factorial of the number// Display the result// Initialize the result as 1 since factorial starts from 1// Prompt the user to enter a number

Create a free account to access the full topic