我是一个c语言初学者,有问题请教前辈。

来源:百度知道 编辑:UC知道 时间:2024/09/22 22:34:57
有二道题, 不会做,请帮帮忙!谢谢。
读入1个正实数eps,计算输出下式的值,精确到最后一项的绝对值小于eps(保留6位小数)。用while语句实现循环。s=1-1/4+1/7-1/10+1/13-1/16+...
输入输出示例:
Input eps:1E-4
s=0.835699
另一道:
求1, 1, 2, 3,5,8,13的前N项和,用while循环。
谢谢了!!!!!

第一道题:
#include "stdio.h"
main()
{float eps,s=0,i=4,k=1;
int f=1;
printf("\ninput eps:");
scanf("%f",&eps);
while(f*k>eps)
{s+=k;f*=-1;k=f*(1.0/i);i+=3;}
printf("s=%f",s);
getch();
}

第二道题:
#include"stdio.h"
main()
{int f1=1,f2=1,f3,n,i=3;
long s=2;
printf("\ninput the number N :");
scanf("%d",&n);
printf("\n%d+%d+",f1,f2);
while(i<=n)
{f3=f1+f2;f1=f2;f2=f3;s+=f3;
if(i==n)printf("%d=",f3)
else printf("%d+",f3);
i+=1;
}
printf("%ld\n",s);
getch();
}