资源描述:
《c语言循环结构程序设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1、求147……100之和。解法1:main(){ints,i;s=0;for(i=1;i<=100;i=3) s=si;printf("s=%d",s);}解法2:main(){ints,i; s=0;i=1;while(i<=100){s=si; i=3;}printf("s=%d",s);}2、求1到100之间的奇数之和与偶数之和。解法1:main(){ints1,s2,i;s1=s2=0;for(i=1;i<=100;i){if(i%2==1) s1=s1i; /*奇数之和*/ else s2=s2i;
2、 /*偶数之和*/ }printf("s1=%d,s2=%d",s1,s2);}解法2:main(){ints1,s2,i;s1=s2=0;for(i=1;i<=99;i=2) s1=s1i; /*奇数之和*/for(i=2;i<=100;i=2) s2=s2i; /*偶数之和*/ }printf("s1=%d,s2=%d",s1,s2);}解法3:main(){ints1,s2,i;s1=s2=0;i=1;while(i<=99){s1=s1i; /*奇数之和*/ i; s2=s2i; /*偶数之和*/ i
3、;}printf("s1=%d,s2=%d",s1,s2);}3、用循环程序求10的阶乘。main(){longp; /*int型取值范围太小*/inti;p=1; /*不能写作p=0;*/for(i=1;i<=10;i) p=p*i;printf("p=%ld",p);}4、求1*3*5*...*19之积。main(){floatp;inti;p=1;for(i=1;i<=19;i=2) p=p*i;printf("p=%f",p);}5、从键盘输入一个正整数n,求123...n之和并输出。main(){inti,n;
4、longs;s=0;scanf("%d",&n);for(i=1;i<=n;i) s=si;printf("s=%ld",s);}6、从键盘输入一个正整数,求出其阶乘并输出。解法1:main(){floatp;inti,k;p=1;scanf("%d",&k);for(i=1;i<=k;i) p=p*i;printf("p=%f",p);}解法2:main(){floatp;intk;p=1;scanf("%d",&k);while(k>=1){p=p*k; k--;}printf("p=%f",p);}6A、求1-1/3
5、1/5-1/7...-1/991/101之和。解法1:main(){floats1,s2,s;inti;s1=s2=0;for(i=1;i<=101;i=4) s1=s11.0/i; /*正数之和*/for(i=3;i<=99;i=4) s2=s21.0/i; /*负数之和*/s=s1-s2;printf("s=%f",s);}解法2:main(){inti,p;floats;s=0;p=1; for(i=1;i<=101;i=2) {s=sp*1.0/i; /*p用于控制累加项的正负*/ p=-p; /*改
6、变正负号*/ }printf("s=%f",s);}6B、求202122...263之和。解法1:#includemain(){floats;inti;s=0;for(i=0;i<=63;i) s=spow(2,i); /*2的i次幂*/printf("s=%f",s);}解法2:main(){floats,t;inti;s=0;t=1;for(i=0;i<=63;i) {s=st; t=t*2; }printf("s=%f",s);}7、求123252...992之和。main(){longs;inti;s
7、=0;for(i=1;i<=99;i=2) s=si*i;printf("s=%ld",s);}8、求11/31/5...1/99之和。main(){inti;floats;s=0;for(i=1;i<=99;i=2) s=s1.0/i; /*不能写作1/i*/printf("s=%f",s);}9、求11/31/5...的前20项之和。main(){inti;floats;s=0;for(i=1;i<=20;i) s=s1.0/(2*i-1); /*不能写作1/(2*i-1)*/printf("s=%f",s);}
8、10、求11/31/5...之和,直到某一项的值小于10-6时停止累加。main(){longn; /*不能写作intn*/floats;s=0;n