文档详情

C语言练习题(带详解答案解析)34943.doc

发布:2018-10-20约7.03千字共17页下载文档
文本预览下载声明
WORD文档下载可编辑 专业资料分享 编程题 1.输入2个整数,求两数的平方和并输出。 #include stdio.h int main(void) { intt a ,b,s; printf(please input a,b:\n); scanf(%d%d”,a,b); s=a*a+b*b; printf(the result is %d\n,s); return 0; } 2. 输入一个圆半径r,当r=0时,计算并输出圆的面积和周长,否则,输出提示信息。 #include stdio.h #define PI 3.14 int main(void) { double r ,area , girth; printf(please input r:\n); scanf(%lf,r); if (r=0) { area =PI*r*r; girth =2*PI*r ; printf(the area is %.2f\n, area); printf(the girth is %.2f\n, girth);} else printf(Input error!\n); return 0; } 3、已知函数y=f(x),编程实现输入一个x值,输出y值。 2x+1 (x0) y= 0 (x=0) 2x-1 (x0) #include stdio.h void main() { int x,y; scanf(“%d”,x); if(x0) y=2*x+1; else if(x0) y=2*x-1; else y=0; printf(“%d”,y); } 4.从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A;80≤score90,等级为B;70≤score80,等级为C;60≤score70,等级为D;score60,等级为E。 #include stdio.h void main(){ int data; char grade; printf(Please enter the score:); scanf(%d”, data); switch(data/10) { case 10: case 9 : grade=’A’; break; case 8: grade=’B’; break; case 7: grade=’C’; break; case 6: grade=’D’; break; default: grade=’E’; } printf(the grade is %c”,grade); } 5. 编一程序每个月根据每个月上网时间计算上网费用,计算方法如下: 要求当输入每月上网小时数,显示该月总的上网费用(6分) #include stdio.h void main() { int hour; float fee; printf(“please input hour:\n”); scanf(“%d”,hour); if(hour=10) fee=30; else if(hour=10hour=50) fee=3*hour; else fee=hour*2.5; printf(“The total fee is %f”,fee); } 6. 从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。 #include stdio.h void main( ) { int a, i,p=0,n=0,z=0; printf(please input number); for(i=0;i10;i++){ scanf(%d,,a); if (a0) p++; else if (a0) n++; else z++; } printf(正数:%5d, 负数:%5d,零:%5d\n,p,n,z); } 7、编程序实现求1-10之间的所有数的乘积并输出。 #include stdio.h
显示全部
相似文档