资源描述:
《C语言课后习题参考答案.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第2章部分习题参考答案一、单项选择题:DABDDDAAABBCD第3章部分习题参考答案一、单项选择题:CACBBCDBBBADCCBDCCBB二、填空题:a!=ba<=cx>20&&x<30x<-1000非01x%3==0&&x%7==0或x%21==0三、编程题2、#include#includevoidmain(){floata,b,c,p,s;printf("请输入三角形的三边长:");scanf("%f%f%f",&a,&b,&c);if(a+b>c&&a+c>b&&b+c>a){p=(a+b+c)/
2、2;s=sqrt(p(p-a)(p-b)(p-c));printf("三角形面积为:%.2f",s);}elseprintf("输入的边长不能构成三角形!");}第3章习题3、#includevoidmain(){intx,y;scanf("%d",&x);if(x>-5&&x<0)y=x-1;elseif(x==0)y=x;elseif(x>0&&x<8)y=x+1;elsey=10;printf("y=%d",y);}if(x>-5&&x<0)y=x-1;if(x==0)y=x;if(x>0&&x<8)y=x
3、+1;if(x<=-5x>=8)y=10;if(x<=-5x>=8)y=10;elseif(x<0)y=x-1;elseif(x==0)y=x;elsey=x+1;第3章习题4、键盘输入一字符,大写转小写,小写转大写,其他原样输出。#includevoidmain(){charch;scanf("%c",&ch);if(ch>='A'&&ch<='Z')ch=ch+32;elseif(ch>='a'&&ch<='z')ch=ch-32;printf("%c",ch);}ch=getchar();if(ch>='A'&&c
4、h<='Z')printf("%c",ch+32);elseif(ch>='a'&&ch<='z')printf("%c",ch-32);elseprintf("%c",ch);第3章习题8.#includevoidmain(){intyear,flag=1;doublemoney,rate,total;printf("请输入存款本金和年限:");scanf("%lf%d",&money,&year);if(year==1)rate=0.00215;elseif(year==2)rate=0.0023;elsei
5、f(year==3)rate=0.00245;elseif(year==5)rate=0.00275;elseif(year==8)rate=0.0032;elseflag=0;if(flag==1){total=money(1+rate12year);printf("总金额为:%.2lf",total);}elseprintf("输入的期限不正确!");}第3章习题#includevoidmain(){intyear,flag=1;doublemoney,rate,total;printf("请输入存款本金和年限:
6、");scanf("%lf%d",&money,&year);switch(year){case1:rate=0.00215;break;case2:rate=0.0023;break;case3:rate=0.00245;break;case5:rate=0.00275;break;case8:rate=0.0032;break;default:printf("输入的期限不正确!");flag=0;}if(flag==1){total=money(1+rate12year);printf("总金额为:%.2lf",total);}}
7、第4章部分习题参考答案一、单项选择题BCDBDBCDBBCCCCCBACDD二、编程题2.编程输出1~200之间所有能被3整除、且个位数为6的整数。方法2:#includevoidmain(){inti;for(i=6;i<=200;i=i+10)if(i%3==0)printf("%5d",i);printf("");}方法1:#includevoidmain(){inti;for(i=1;i<=200;i++)if(i%3==0&&i%10==6)printf("%5d",i);printf("
8、");}3.编程从键盘输入k的值及k个整数,统计其中的正数、零和负数的个数。#includevoidmain(){intx,k,i,p=0,z=0