资源描述:
《c语言练习试题[带详解答案解析]34943》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、.WORD.格式.一、编程题1.输入2个整数,求两数的平方和并输出。#includeintmain(void){intta,b,s;printf("pleaseinputa,b:");scanf("%d%d”,&a,&b);s=a*a+b*b;printf("theresultis%d",s);return0;}2.输入一个圆半径r,当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。#include#definePI3.14intmain(void){
2、doubler,area,girth;printf("pleaseinputr:");scanf("%lf",&r);if(r>=0){area=PI*r*r;girth=2*PI*r;.专业资料.整理分享..WORD.格式.printf("theareais%.2f",area);printf("thegirthis%.2f",girth);}elseprintf("Inputerror!");return0;}3、已知函数y=f(x),编程实现输入一个x值,输出y值。2x+1(x<0)
3、y=0(x=0)2x-1(x>0)#includevoidmain(){intx,y;scanf(“%d”,&x);if(x<0)y=2*x+1;elseif(x>0)y=2*x-1;elsey=0;printf(“%d”,y);}4.从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A;80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;score<60,等级为E。.专业资料.整理分享..WORD.格式
4、.#includevoidmain(){intdata;chargrade;printf("Pleaseenterthescore:");scanf("%d”,&data);switch(data/10){case10:case9:grade=’A’;break;case8:grade=’B’;break;case7:grade=’C’;break;case6:grade=’D’;break;default:grade=’E’;}printf("thegradeis%c”,grade);
5、}5.编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:要求当输入每月上网小时数,显示该月总的上网费用(6分).专业资料.整理分享..WORD.格式.#includevoidmain(){inthour;floatfee;printf(“pleaseinputhour:”);scanf(“%d”,&hour);if(hour<=10)fee=30;elseif(hour>=10&&hour<=50)fee=3*hour;elsefee=hour*2.5;printf(“Th
6、etotalfeeis%f”,fee);}6.从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。#includevoidmain(){inta,i,p=0,n=0,z=0;printf("pleaseinputnumber");for(i=0;i<10;i++){scanf("%d,",&a);.专业资料.整理分享..WORD.格式.if(a>0)p++;elseif(a<0)n++;elsez++;}printf("正数:%5d,负数:%5d,零:%5d",p,n
7、,z);}7、编程序实现求1-10之间的所有数的乘积并输出。#includevoidmain(){inti;longsum=1;for(i=1;i<=10;i=i+1)sum=sum*i;printf(“thesumofoddis:%ld”,sum);}8.从键盘上输入10个数,求其平均值。#includevoidmain(){inta,i,sum=0;floatave;;for(i=0;i<10;i++){scanf("%d",&a);.专业资料.整理分享..WORD
8、.格式.sum+=a;}ave=(float)sum/10;printf("ave=%f",ave);}9、编程序实现求1-1000之间的所有奇数的和并输出。#includevoidmain(){inti,sum=0;for(i=1;i<1000;i=i+2)sum=sum+i;printf(“thesumofoddis:%d”,sum);}10.有一个分数序列:2/1,3/2,5/3,8/5,13/8,