Record the code life: data algorithm (find the factorial of a number) Java language

To find the factorial of an integer, we must first understand how the factorial is obtained. For example, n=5, then the factorial of n is 5×4×3×2×1, which is the factorial of n, because we use a for loop to traverse, This effect is achieved by decrementing.
(The following is the code↓)

import java.util.Scanner;
public class Factorial{
    
    
	public static void main(String[] args){
    
    
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		int a = input.nextInt();
		for(int i = a - 1 ; i > 0 ; i--){
    
    
			a *= i;
		}
		System.out.println("该数的阶乘为:"+a);
	}
}

Guess you like

Origin blog.csdn.net/qq_40266349/article/details/108567738