while循环嵌套写1!+2!+3!+......+n!
时间: 2023-09-27 18:04:23 浏览: 48
n = int(input("请输入一个正整数:"))
i = 1
while i <= n:
j = 1
result = 1
while j <= i:
result *= j
j += 1
print("{}! = {}".format(i, result))
i += 1
相关问题
c语言while循环嵌套写1!+2!+3!+......+n!
#include <stdio.h>
int main() {
int n, i = 1, factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
int j = 1, f = 1;
while (j <= i) {
f *= j;
j++;
}
printf("%d! = %d\n", i, f);
i++;
}
return 0;
}
循环嵌套示例及练习2:求1!+2!+3!+4!+5!(for循环嵌套while循环)
循环嵌套示例:
```
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
```
输出结果为:
```
*
**
***
****
*****
```
练习2代码如下:
```
int sum = 0;
int i = 1;
while (i <= 5) {
int j = 1;
int factorial = 1;
while (j <= i) {
factorial *= j;
j++;
}
sum += factorial;
i++;
}
printf("1!+2!+3!+4!+5!=%d\n", sum);
```
输出结果为:
```
1!+2!+3!+4!+5!=153
```
阅读全文