在同一个程序中用3种循环分别计算1+2+3+…+100
时间: 2023-10-08 17:04:44 浏览: 35
使用while循环:
```
i = 1
while i <= 100:
print(i)
i += 1
```
使用for循环:
```
for i in range(1, 101):
print(i)
```
使用do-while循环(Python中没有do-while循环,可以用while True + break来模拟):
```
i = 1
while True:
print(i)
i += 1
if i > 100:
break
```
相关问题
C语言在同一个程序中用3种循环分别计算1+2+3+…+100
1. 使用for循环:
```c
#include <stdio.h>
int main() {
int i;
printf("使用for循环计算1~100:\n");
for (i = 1; i <= 100; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
```
2. 使用while循环:
```c
#include <stdio.h>
int main() {
int i = 1;
printf("使用while循环计算1~100:\n");
while (i <= 100) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
```
3. 使用do-while循环:
```c
#include <stdio.h>
int main() {
int i = 1;
printf("使用do-while循环计算1~100:\n");
do {
printf("%d ", i);
i++;
} while (i <= 100);
printf("\n");
return 0;
}
```
c语言在同一个程序中用3种循环分别计算1+2+3+…+100
1. 使用for循环:
```c
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 100; i++) {
printf("%d ", i);
}
return 0;
}
```
2. 使用while循环:
```c
#include <stdio.h>
int main() {
int i = 1;
while (i <= 100) {
printf("%d ", i);
i++;
}
return 0;
}
```
3. 使用do-while循环:
```c
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 100);
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)