c语言中三种循环方式计算1*1+3*3+5*5+…+(2*n-1)*(2*n-1)的前50项结果。
时间: 2023-12-18 13:28:12 浏览: 82
以下是三种循环方式计算1*1+3*3+5*5+…+(2*n-1)*(2*n-1)的前50项结果的C语言程序:
1. for循环
```c
#include <stdio.h>
int main() {
int n = 50;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += (2 * i - 1) * (2 * i - 1);
}
printf("前50项结果为:%d\n", sum);
return 0;
}
```
2. while循环
```c
#include <stdio.h>
int main() {
int n = 50;
int sum = 0;
int i = 1;
while (i <= n) {
sum += (2 * i - 1) * (2 * i - 1);
i++;
}
printf("前50项结果为:%d\n", sum);
return 0;
}
```
3. do-while循环
```c
#include <stdio.h>
int main() {
int n = 50;
int sum = 0;
int i = 1;
do {
sum += (2 * i - 1) * (2 * i - 1);
i++;
} while (i <= n);
printf("前50项结果为:%d\n", sum);
return 0;
}
```
阅读全文