階乗の計算
#お題
整数 N が与えられます。
N の階乗 N!
を計算して出力してください。
#入力
N
・ 1 行で整数 N が与えられます。
#出力
N の階乗 N!
を計算して出力してください。
#コード
n = int(input())
ans = n
while n -1 > 0:
ans = ans * (n - 1 )
n -= 1
print(ans)
↓(修正)
import math
n = int(input())
print(math.factorial(n))
#参考
- Python の場合、 math モジュールの factorial 関数を使用すると、階乗の計算を行うことができます。
from math import factorial
N = int(input())
print(factorial(N))