1+1⼀2+2⼀3+3⼀5+5⼀8……前20项的和C语言程序

2024-11-26 23:45:27
推荐回答(3个)
回答1:

#include
main()
{
float numerator = 1, denominator = 2, temp;
float total = 0;
int num;
total += 1;//第一项
for( num=0; num<19; num++ )//后十九项
{
total += numerator / denominator;
temp = denominator;
denominator += numerator;
numerator = temp;
}
printf("total = %f\n",total);
}

VC编译通过,希望对你有点帮助。

回答2:

main()
{
int a,b,i,t;
double sum,x;
a = 1;
b = 1;
sum = 1.0;
for(i = 1;i < 20;i++){
x = b /(double)(a + b);
sum += x;
t = b;
b = a + b;
a = t;
}
printf("Total:%f",sum);
getch();
}

稍微做了修改

回答3:

TC版:
main()
{
int a,b,i,t;
double sum,x;
a = 1;
b = 1;
sum = 1.0;
for(i = 1;i < 20;i++){
x = b /(a + b);
sum += x;
t = b;
b = a + b;
a = t;
}
printf("Total:%f",sum);
getch();
}