使用c语言完成用while和do-while循环计算11+23+35+…+n(2*n-1)的前50项结果。
时间: 2023-10-24 10:14:13 浏览: 51
```c
#include <stdio.h>
int main() {
int n = 1, count = 0;
printf("前50项如下:\n");
while (count < 50) {
printf("%d ", 2*n-1);
n++;
count++;
}
printf("\n");
n = 1, count = 0;
printf("前50项如下:\n");
do {
printf("%d ", 2*n-1);
n++;
count++;
} while (count < 50);
return 0;
}
```
相关问题
用while和do-while循环计算1*c语言:1+2*3+3*5+…+n*(2*n-1)的前50项结果。
使用while循环:
```c
#include <stdio.h>
int main() {
int n = 1;
int count = 0;
while(count < 50) {
printf("%d ", n * (2 * n - 1));
n++;
count++;
}
return 0;
}
```
使用do-while循环:
```c
#include <stdio.h>
int main() {
int n = 1;
int count = 0;
do {
printf("%d ", n * (2 * n - 1));
n++;
count++;
} while(count < 50);
return 0;
}
```
用C语言分别使用while和do-while循环编程计算n的阶乘。
好的,这是一个关于编程的问题,我可以回答。以下是使用while循环和do-while循环分别计算n的阶乘的C语言代码:
使用while循环:
```
#include <stdio.h>
int main() {
int n, i = 1, fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
fact *= i;
i++;
}
printf("%d! = %d\n", n, fact);
return 0;
}
```
使用do-while循环:
```
#include <stdio.h>
int main() {
int n, i = 1, fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
do {
fact *= i;
i++;
} while (i <= n);
printf("%d! = %d\n", n, fact);
return 0;
}
```
其中,while循环首先判断条件是否成立,如果成立则执行循环体;do-while循环先执行循环体,再判断条件是否成立。在计算阶乘时,两者的结果是一样的。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)