编写程序求 number 的阶乘。number 的阶乘等于1*2*...*number,比如,5的阶乘等于1*2*3*4*5,结果为120。Python语言用for循环
时间: 2023-10-21 16:15:00 浏览: 70
即可实现,代码如下:
```
number = int(input("请输入一个整数:"))
factorial = 1
for i in range(1, number+1):
factorial *= i
print(number,"的阶乘是:",factorial)
```
请注意,输入的应该是一个正整数,否则程序会报错。
相关问题
编写程序求 number 的阶乘。number 的阶乘等于1*2*...*number,比如,5的阶乘等于1*2*3*4*5,结果为120。Python语言
可以使用递归来计算 number 的阶乘。下面是使用 Python 语言编写的程序:
```
def factorial(number):
if number == 1:
return 1
else:
return number * factorial(number - 1)
# 测试
print(factorial(5)) # 输出 120
```
C语言计算n的阶乘;计算1*3*5*7*....*(2n+1)
Sure, I can help you with that. Here's a code snippet in C language to calculate the factorial of n:
```c
#include <stdio.h>
int main() {
int n, i, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= (2*i - 1);
}
printf("Factorial of %d is %d\n", n, factorial);
return 0;
}
```
This program uses a `for` loop to iterate through the odd numbers (`2n-1`) up to `2n-1`, and multiplies them together to calculate the factorial of n.
阅读全文