Package Practicego;import java.util.scanner;/* * 4. The user enters a number within 10, and the factorial of the number is calculated by Operation */public class Cto {public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.println ("Please enter a number within 10, calculated by the system factorial"); int num = Sc.nextint (); int result = 1;int store = num;//Avoid direct manipulation of input values for (int i=0 ; i<num; i++) {result *= store;store--;} System.out.println (num+ "factorial is:" +result);}}
Test:
Please enter a number within 10, and the factorial of factorial 00 is calculated by the system: 1
Mathematically stated: 0! = 1 with the above code just ok!
Please enter a number within 10, and the factorial of factorial 11 is calculated by the system: 1
Please enter a number within 10, and the factorial of factorial 55 is calculated by the system: 120
Java Loop Exercise: The user enters a number within 10 to calculate the factorial of the number by operation