编程计算S=1!+2!+3!+4!+…+10!的值。语言代码
时间: 2023-11-24 18:51:33 浏览: 105
以下是Python代码,用于计算S=1!+2!+3!+4!+…+10!的值:
```python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
S = 0
for i in range(1, 11):
S += factorial(i)
print(S)
```
输出结果为:4037913
相关问题
C++语言编程计算s=1! +2! +3! +……n! n是正整数
以下是计算s的C语言代码:
```c
#include <stdio.h>
int main() {
int n, i, j, factorial = 1;
double s = 0;
printf("请输入一个正整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
s += factorial;
}
printf("s = %.0f\n", s);
return 0;
}
```
这个程序会让用户输入一个正整数n,然后计算出s的值,并输出结果。
masm编程计算S=1!+2!+3!+…+5!并显示结果
以下是masm汇编语言计算S=1!2!+3!+…+5!并显示结果的代码:
```
DATA SEGMENT
sum DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, 1
MOV BX, 1
MOV CX, 1
MOV DX, 1
MOV SI, 1
MOV sum, 0
CALL FACTORIAL
ADD sum, AX
MOV SI, 2
MOV sum, 0
CALL FACTORIAL
ADD sum, AX
MOV SI, 3
MOV sum, 0
CALL FACTORIAL
ADD sum, AX
MOV SI, 4
MOV sum, 0
CALL FACTORIAL
ADD sum, AX
MOV SI, 5
MOV sum, 0
CALL FACTORIAL
ADD sum, AX
MOV AX, sum
CALL DISPLAY
MOV AH, 4CH
INT 21H
FACTORIAL PROC
MOV DX, SI
MOV AX, SI
DEC DX
MUL DX
DEC DX
MUL DX
DEC DX
MUL DX
DEC DX
MUL DX
RET
FACTORIAL ENDP
DISPLAY PROC
MOV BX, 10
MOV CX, 0
MOV DX, 0
DIV BX
ADD DL, 48
MOV CX, DX
MOV DL, 0AH
DIV BX
ADD DL, 48
MOV DH, CL
MOV DL, 0AH
MOV AH, 2
INT 21H
MOV DL, DH
ADD DL, 48
MOV AH, 2
INT 21H
MOV DL, CL
ADD DL, 48
MOV AH, 2
INT 21H
MOV DL, ' '
MOV AH, 2
INT 21H
RET
DISPLAY ENDP
CODE ENDS
END START
```
阅读全文