资源描述:
《c语言程序设计 (何钦铭 颜晖 著) 高等教育出版社第二章 课后答案》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、练习2-8#includevoidmain(){floatfahr,celsius;scanf("%f",&fahr);celsius=5.0/9*(fahr-32);printf("fahr=%f,celsius=%f",fahr,celsius);}练习2-9#includevoidmain(){inta,b,c;floatavg;scanf("%d%d%d",&a,&b,&c);avg=(a+b+c)/3.0;printf("a=%d,b=%d,c=%d,avg=%f",a,b,c,avg);}练习2-10#include
2、voidmain(){floatx,y;scanf("%f",&x);if(x==0)y=0;elsey=1/x;printf("x=%.3f,y=%.3f",x,y);}练习2-11#include#includevoidmain(){floatx,y;scanf("%f",&x);if(x>=0)y=sqrt(x);elsey=pow(x,5)+2*x+1/x;printf("x=%.2f,y=%.2f",x,y);}练习2-12#includevoidmain(){intlower,upper,fahr;floatcelsi
3、us;scanf("%d%d",&lower,&upper);printf("fahr----celsius");for(fahr=lower;fahr<=upper;fahr=fahr+2){celsius=5.0/9*(fahr-32);printf("%d%10.2f",fahr,celsius);}}练习2-13#includevoidmain(){intn,i;floatsum=0;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+1.0/i;printf("sum=%.2f",sum);}练习2-14#include<
4、stdio.h>voidmain(){intn,i;floatsum=0;scanf("%d",&n);for(i=1;i<=2*n-1;i=i+2)sum=sum+1.0/i;printf("sum=%.2f",sum);}练习2-15#includevoidmain(){intn,i;floatsum=0.0,flag=1.0,d=1.0;scanf("%d",&n);for(i=1;i<=n;i++){sum=sum+flag/d;d=d+3;flag=-flag;}printf("sum=%.2f",sum);}练习2-16#include5、>voidmain(){intm,n,i;doublesum=0.0,product=1.0;scanf("%d%d",&m,&n);for(i=1;i<=m;i++)product=product*i;sum=sum+product;product=1.0;for(i=1;i<=n;i++)product=product*i;sum=sum+product;printf("m!+n!=%lf",sum);}练习2-18#include#includevoidmain(){intn,i;scanf("%d",&n);for(i=0;i<=n;i++)p
6、rintf("%10d%10.2f",i,pow(3,i));练习2-19#includedoublefact(intn);voidmain(){inti,n;scanf("%d",&n);for(i=1;i<=n;i++)printf("%10d%10.2f",i,fact(i));}doublefact(intn){inti;doubleproduct=1.0;for(i=1;i<=n;i++)product=product*i;returnproduct;}练习2-20#includedoublefact(intn);voidmain(){
7、intm,n;scanf("%d%d",&m,&n);printf("%5d%5d%10.2f",m,n,fact(n)/(fact(m)*fact(n-m)));}doublefact(intn){inti;doubleproduct=1.0;for(i=1;i<=n;i++)product=product*i;returnproduct;}习题2-1#includevoidmain(){intn