循环嵌套示例及练习2:求1!+2!+3!+4!+5!(for循环嵌套while循环)
时间: 2023-11-17 13:01:44 浏览: 99
C语言实现求1+2+3+……+100的和,要求分别用while、do while、for循环实现。.txt
循环嵌套示例:
```
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
```
阅读全文