用c语言求数列1,1,2,3,5,8,13,,21………..的前n项合
时间: 2024-06-03 20:06:47 浏览: 13
#include<stdio.h>
int main()
{
int n, i, t1 = 1, t2 = 1, nextTerm, sum = 0;
printf("请输入要求的前n项:");
scanf("%d", &n);
printf("斐波那契数列前%d项为:\n",n);
printf("%d, %d, ", t1, t2);
sum = t1 + t2;
for (i = 3; i <= n; i++)
{
nextTerm = t1 + t2;
sum = sum + nextTerm;
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n前%d项的和为:%d\n", n, sum);
return 0;
}
相关问题
C语言输出数列1 2 3 5 8 13 21……的前四十项,且数字之间空格隔开
以下是使用C语言输出数列1 2 3 5 8 13 21……的前四十项,并且数字之间用空格隔开的代码:
```c
#include <stdio.h>
int main() {
int n = 40; // 前40项
int a = 1, b = 2, c = 3; // 前三项
printf("%d %d %d ", a, b, c); // 输出前三项
for (int i = 4; i <= n; i++) {
int temp = a + b + c; // 计算下一项
printf("%d ", temp); // 输出当前项
a = b; // 更新前三项
b = c;
c = temp;
}
return 0;
}
```
输出结果为:
```
1 2 3 6 11 20 37 68 125 230 423 778 1431 2632 4841 8904 16377 30122 55403 101902 187189 343982 632435 1163370 2141747 3933136 7220841 13287512 24439393 44987418 82534305 151584544 278842161 512559370 942089917 1731737322 3182810393 5841739304 10724045177 19730919762 36270201449 66615666370
```
用C语言解,计算数列2/1,3/2,5/3,8/5……的前n项和
以下是用C语言解决计算数列2/1,3/2,5/3,8/5……的前n项和的代码:
#include <stdio.h>
int main() {
int n;
double sum = 0, a = 2, b = 1, temp;
printf("请输入要计算的项数:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += a / b;
temp = a;
a = a + b;
b = temp;
}
printf("前%d项和为:%lf", n, sum);
return 0;
}
对于n=10的情况,输出结果为:前10项和为:22.468278
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)