




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. 要求在屏幕上输出下一行信息。This is a c program.程序:#includeint main()printf(“this is a c program.n”);return 0;2. 求两个整数之和。程序:#includeint main()int a,b,sum;a=122;b=234;sum=a+b;printf(“sum is %dn”,sum);return 0;3. 求两个整数之间的较大者。程序:#includeint main()int max(int x,int y);int a,b,c;scanf(%d,%d,&a,&b);c=max(a,b);printf
2、(max=%dn,c);return 0;int max(int x,int y)int z;if(xy)z=x;else z=y;return(z);4. 有人用温度计测量出华氏发表示的温度(如69F),今要求把她转换成以摄氏法表示的温度(如20)。公式:c=5(f-32)/9.其中f代表华氏温度,c代表摄氏温度。程序:#includeint main()float f,c;f=64.0;c=(5.0/9)*(f-32);printf(f=%fnc=%fn,f,c);return 0;5. 计算存款利息。有1000元,想存一年。有一下三种方法可选:(1)活期:年利率为r1;(2)一年定期:年
3、利率为r2;(3)存两次半年定期:年利率为r3。分别计算一年后按三种方法所得到的本息和。程序:#includeint main()float p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;p1=p0*(1+r1);p2=p0*(1+r2);p3=p0*(1+r3/2)*(1+r3/2);printf(p1=%fnp2=%fnp3=%fn,p1,p2,p3);return 0;6. 给定一个大写字母,要求以小写字母输出。程序:#includeint main()char c1,c2;c1=A;c2=c1+32;printf(“%cn”,c2);pr
4、intf(“%dn”,c2);return 0;7. 给出三角形的三边长,求三角形的面积。公式:若给定三角形的三边长,且任意两边之长大于第三边。则:area=ss-as-b(s-c)其中s=(a+b+C)/2.程序:#include#includeint main()double a,b,c,area;a=3.67;b=5.43;c=6.21;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c);printf(“a=%ftb=%ftc=%fn”,a,b,c);printf(“area=%fn”,area);return 0;8. 求ax2+bx+c=0方程的根。a
5、,b,c由键盘输入,设b2-4ac0.程序:#include#includeint main()double a,b,c,disc,x1,x2,p,q;scanf(“%lf%lf%lf”,&a,&b,&c);disc=b*b-4*a*c;if(disc0)printf(“This question has no real rootsn”);elsep=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf(“x1=%7.2fnx2=%7.2fn”,x1,x2);return 0;9. 先后输出BOY三个字符。程序:#includeint mai
6、n()char a=B,b=O,c=Y;putchar(a);putchar(b);putchar(c);putchar(n);return 0;10. 用三个getchar函数先后向计算机输入BOY三个字符,然后用putchar函数输出。程序:#includeint main()char a,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c);putchar(n);return 0;或#includeint main()putchar(getchar();putchar(getchar();putc
7、har(getchar();putchar(n);return 0;11. 用getchar函数从键盘读入一个大写字母,把它转换成小写字母,然后用getchar函数输出对应的小写字母。程序:#includeint main()char c1,c2;c1=getchar();c2=c1+32;putchar(c2);putchar(n);return 0;12. 输入两个实数,按代数值由小到大的顺序输出这两个数。(参照将两个杯子中的水互换,必须借助第三个杯子)。程序:#includeint main()float a,b,t;scanf(“%f,%f”,&a,&b);if(ab)t=a;a=b;
8、b=t;printf(“%5.2f,%5.2fn”,a,b);return 0;13. 输入a,b,c三个数,要求由小到大的顺序输出。程序:#includeint main()float a,b,c,t;scanf(%f,%f,%f,&a,&b,&c);if(ab);t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(bc)t=b;b=c;c=t;printf(%5.2f,%5.2f,%5.2fn,a,b,c);return 0;14输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是,不转换。然后输出最后得到的字符。程序:#includeint main()
9、char ch;scanf(“%c”,&ch);ch=(ch=A&ch=Z)?(ch+32):ch;printf(“%cn”,ch);return 0;或#includeint main()char ch;scanf(%c,&ch);if(ch=A&ch=Z)printf(%cn,ch+32);elseprintf(%cn,ch);return 0;15.有一个函数:y=-1 (x0 编一程序。输入一个x的值,要求输出相应的y值。程序:#includeint main()int x,y;scanf(%d,&x);if(x0)y=-1;elseif(x=0)y=0;elsey=1;printf(
10、x=%dny=%dn,x,y);return 0;16.要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70-84分。C等为60-69分,D等为60分以下。成绩的等级由键盘输入。程序:#includeint main()char grade;scanf(%c,grade);printf(You score:n);switch(grade)caseA:printf(85100n);break;caseB:printf(7084n);break;caseC:printf(6069n);break;caseD:printf(60n);break;default:printf(ente
11、r date errorn);return 0;17.写一程序,判断某一年是否为闰年。程序:#includeint main()int leap,year;printf(please enter year:);scanf(%d,&year);if(year%4=0)if(year%100=0)if(year%400=0)leap=1;elseleap=0;else leap=1;else leap=0;if(leap)printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);return 0;或#includ
12、eint main()int leap,year;printf(please enter year:);scanf(%d,&year);if(year%4!=0)leap=0;else if(year%100!=0)leap=1;else if(year%400!=0)leap=0;elseleap=1;if(leap=1)printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);return 0;或#includeint main()int leap,year;printf(please enter yea
13、r:);scanf(%d,&year);if(year%4=0&year%100!=0)|(year%400=0)leap=1;elseleap=0;if(leap=1)printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);return 0;18. 求ax2+bx+c=0方程的根。a,b,c由键盘输入。(完整版)程序:#include#includeint main()double a,b,c,disc,x1,x2,x3,realpart,imagepart;scanf(%lf,%lf,%lf,&a,&b
14、,&c);printf(The equation);if(fabs(a)=1e-6)printf(is not a quadratic);elsedisc=b*b-4*a*c;if(fabs(disc)1e-6)printf(has two distinct real rootsn%8.4f,%8.4f,x1,x2);x1=(-b+sqrt(disc)/2*a;x2=(-b-sqrt(disc)/2*a;elserealpart=-b/2*a;imagepart=sqrt(-disc)/2*a;printf(has two complex roots:n);printf(%8.4f+%8.4f
15、in,realpart,imagepart);printf(%8.4f-%8.4fin,realpart,imagepart);return 0;/注释:由于b*b-4*a*c(disc)是实数,而实数在计算和存储时会有一些微小的误差,因此不能直接进行如下判断:/“if(disc=0),因为这样可能出现本来是零的量,由于上述误差而判别为不等于零而导致结果错误,/所以采取的办法是判别disc的绝对值(fabs(disc)是否小于一个很小的数(例如:1e-6)。如果小于此数,则认为disc=0.19.给出一个不多出5位的正整数,要求:(1):求出它是几位数;(2):分别输出每一位数字;(3):按逆
16、序输出各位数字,例如原数为321,输出123。程序:#includeint main()int num,indiv,ten,hundred,thousand,ten_thousand,place;printf(请输入一个正整数(099999):);scanf(%d,&num);if(num9999)place=5;else if(num999)place=4;else if(num99)place=3;else if(num9)place=2;elseplace=1;printf(位数为:%dn,place);ten_thousand=num/10000;thousand=(num-ten_t
17、housand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);printf(每一个数字分别为:);printf(%d,%d,%d,%d,%dn,ten_thousand,thousand,hundred,ten,indiv);switch(place)case 5:printf(反序数字为:%d%d%d%d%dn,indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf(反序数字为:%d%d%d%dn,indiv,ten,hundred,thousand);break;case 3:printf(反序数字为:%d%d%dn,indiv,ten,hundred);break;case 2:printf(反序数字为:%d%dn,indiv,ten);break;case 1:printf(反序数字为:%dn,indiv);break;return 0;20.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年河北科技学院单招职业适应性测试题库含答案
- 2025年度手车交易协议书(个人二手车鉴定评估)
- 2025年度火锅餐饮品牌区域代理合作协议
- 2025年度农庄土地租赁与经营权转让合同
- 2025年度影视后期剪辑团队演职人员聘用合同
- 2025年广西生态工程职业技术学院单招职业倾向性测试题库审定版
- 2025年度交通事故私下和解赔偿合同范本
- 2025年度房产抵押债务重组执行合同
- 2025年度人工智能助手软件许可授权协议
- 2025年度房屋地基打桩与绿色建材采购合同
- 2023春下册五年级语文《每课生字预习表》
- 车间领班求职简历
- 八年级下物理校本作业(人教版)课时作业
- 教科版三年级科学下册分组实验与演示实验目录
- 急性肾小球肾炎讲稿
- 05G359-3 悬挂运输设备轨道(适用于一般混凝土梁)
- (完整版)《城市轨道交通应急处理》课程标准
- 2023年江苏农牧科技职业学院单招职业适应性测试题库及答案解析
- 毛泽东诗词鉴赏分析
- 量具检具清单
- 江苏市政工程计价表定额计算规则
评论
0/150
提交评论