C语言课后答案word版本

上传人:re****.1 文档编号:487366889 上传时间:2022-11-05 格式:DOCX 页数:22 大小:24.81KB
返回 下载 相关 举报
C语言课后答案word版本_第1页
第1页 / 共22页
C语言课后答案word版本_第2页
第2页 / 共22页
C语言课后答案word版本_第3页
第3页 / 共22页
C语言课后答案word版本_第4页
第4页 / 共22页
C语言课后答案word版本_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《C语言课后答案word版本》由会员分享,可在线阅读,更多相关《C语言课后答案word版本(22页珍藏版)》请在金锄头文库上搜索。

1、学习资料第 3 章三、编程题1 编写程序,输入一个非负数,输出以此数为半径的圆周长以及面积。#include stdio.h#define PI 3.1415void main()float r,area,circumference;scanf(%f,&r);area=PI*r*r;circumference=2*r*PI;printf(area=%6.2fncircumference=%6.2fn,area,circumference);2 编写程序,输出下面结果,注意,双引号也要输出:“Im a student!”#include void main()printf(Im a studen

2、t!n);3 编写程序,输入一个小写字母,将其转换为大写字母输出。例如输入b,则输出 B。提示:小写字母和对应的大写字母的ASCII 码值相差 32。void main()char ch;ch=getchar();ch-=32;putchar(ch);/printf(%c,ch);4 编写程序,输入一个华氏温度f ,输出其相应的摄氏温度c。华氏温度和摄氏温度的转换公式为:5c (f 32)9#include void main()float f,c;scanf(“%f”,&f);c=5.0*(f-32)/9;printf(“华氏温度 %5.2f 转换为摄氏温度为:%5.2fn ”,f,c);仅

3、供学习与参考学习资料第 4 章三、编程题1 输入一个整数,判断这个整数是奇数还是偶数(提示:整数的奇偶性可以利用取余运算符 %判定)。 #include void main()int a;scanf(%d,&a);if(a%2)printf( 奇数 n);elseprintf( 偶数 n);2 编写程序,输入一个24 小时制的时间,转换为12 小时制时间后进行输出。以13 点 15 分为例,输入:13:15 ,则输出:下午1:15 。#include void main()int hour,minute;scanf(%d:%d,&hour,&minute);if (hour12)hour=ho

4、ur-12;printf(%d:%dn,hour,minute);3 输入年号,判断它是否是闰年(如果年号能被400 整除,或能被4 整除,而不能被100 整除,则是闰年,否则不是)。void main()int year;scanf(%d,&year);if (year%400=0|(year%4=0&year%100=0)printf(%d 是闰年 n,year);elseprintf(%d 不是闰年 n,year);仅供学习与参考学习资料4 输入一个字符,如果是大写字母则输出对应的小写字母,如果是小写字母则输出相应的大写字母,如果都不是则原样输出。#include void main()

5、char ch;scanf(%c,&ch);if(ch=a&ch=A&ch=Z)ch+=32;printf(n%cn,ch);5 设计一个简单的计算器程序,能输入整型运算数和基本运算符(+,- ,* ,/ ),输出计算结果。例如:输入 2+6,输出 2+6=8。#includemain()float a,b,result;char op;scanf(%f%c%f,&a,&op,&b);switch(op)case+: result=a+b; printf(=%f,result); break;case-: result=a-b; printf(=%f,result); break;case*:

6、 result=a*b; printf(%f,result); break;case/: if(b=0.0)printf(error!n);result=a/b;printf(=%f,result);default:printf(error due to the illegal input!n);第 5 章三、编程题1 编写程序,显示100 200 之间能被 7 除余 2 的所有整数。#include stdio.hmain()int i; for(i=100;i=200;i+) if(i%7=2) printf(t%dt,i);仅供学习与参考学习资料2 输入 n 个整数,求这n 个整数中的最

7、大数、最小数和偶数平均数,并输出。#include void main()int i,n,data,max=0,min=0,even=0,evennumber=0;printf(Please input the number of data:);scanf(%d,&n);printf(Please input the data:n);scanf(%d,&data);max=data;min=data;if (data%2=0)even=even+data;evennumber+;for(i=1;imax)max=data;elseif (datamin)min=data;if (data%2=

8、0)even=even+data;evennumber+;printf(The max is %dnThe min is %dn The average of even is %5.2fn,max,min,(float)(even)/evennumber);3 输入一串字符, 以回车作为结束标志。统计并输出这串字符中大写字母、小写字母和数字字符的个数。#include void main()int upper=0,lower=0,number=0;char letter;while(letter=getchar()!=n)仅供学习与参考学习资料if (letter=a&letter=A&let

9、ter=0&letter=9)number+;printf(the number of uppercase is:%dn,upper);printf(the number of lowercase is:%dn,lower);printf(the number of number is:%dn,number);4 输出九九乘法表。#include void main()int i,j;for(i=1;i=9;i+)for(j=1;j=i;j+)printf(%d*%d=%-3d ,i,j,i*j);printf(n);5 编写程序,输出3 1000 之间全部素数。#include #include math.hvoid main()int k,data,tag;for(data=3;data=1000;data+) /外层循环,用来产生2 1000 之间的整数tag=0;/tag用于表示数i 是否是素数,没有判断前先假定是素数for(k=2;k=3) (即从第三个数起,每个数等于前2 个数之和)。#include stdio.hvoid main()int f40,n;f0=1;f1=1;printf(Fibonacci数列的前 40 个数是: %dt%dt,f0,f1);for(n=2;n40;n+)fn=fn-1+fn-2;prin

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 演讲稿/致辞

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号