HDU - 1042-N!(Java)

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 

Input

One N in one line, process to the end of file. 

Output

For each N, output N! in one line. 

Sample Input

1
2
3

Sample Output

1
2
6

代码:

import java.math.BigDecimal;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			System.out.println(fun(n));
		}
	}
 
	public static BigDecimal fun(int n) {
		BigDecimal s = new BigDecimal(1);
		for (int i = 1; i <= n; i++) {
			BigDecimal a = new BigDecimal(i);
			s = s.multiply(a);
		}
		return s;
	}
 
}

猜你喜欢

转载自blog.csdn.net/lbperfect123/article/details/86567308