要求说明:有一分数列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列前20项之和
//简单的级数运算
#include <stdio.h>
int main(int argc, char **argv)
{
int n,t;
float a = 2,b = 1,sum = 0; //定义变量a, b,sum 单精度型并分别赋初值
for( n = 1; n <= 20; n ++) //循环变量n从1到20
{
sum += a/b; //累加求和
t = a; //借助中间变量完成数值转换
a = a+b;
b = t;
}
printf("sum is %f\n",sum); // 打印输出
return 0;
}
样例输入输出:
sum is 32.660259