用python实现输入n进行累加和阶乘代码简单
时间: 2024-03-13 15:44:42 浏览: 97
python计算n的阶乘的方法代码
5星 · 资源好评率100%
当然,下面是Python代码实现:
累加和:
```python
n = int(input("请输入一个正整数n:"))
sum = 0
for i in range(1, n+1):
sum += i
print("1到n的累加和为:", sum)
```
阶乘:
```python
n = int(input("请输入一个正整数n:"))
factorial = 1
for i in range(1, n+1):
factorial *= i
print("%d! = " % n, factorial)
```
阅读全文