📜  阶乘 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:40.698000             🧑  作者: Mango

代码示例5
//Java program to find factorial of a numberimport java.util.Scanner;public class factorial{        public static void main(String[] args)    {        //scanner class declaration        Scanner sc = new Scanner(System.in);        //input from user        System.out.print("Enter a number : ");                        int number = sc.nextInt();        if(number >= 0)        {            //declare a variable to store factorial            int fac = 1;            for(int i = number ; i >= 1 ; i--)            fac = fac * i;            //display the result            System.out.println("Factorial of "+number+" is "+fac);            //closing scanner class(not compulsory, but good practice)        }        else            System.out.println("Value is not defined, please re-enter the value");        sc.close();                                                        }}