資源描述:
《c語言練習題(帶詳解答案)》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
1、一、編程題1.輸入2個整數(shù),求兩數(shù)的平方和并輸出。#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){doubler,area,girth;printf("pleaseinputr:");scanf(
2、"%lf",&r);if(r>=0){area=PI*r*r;girth=2*PI*r;printf("theareais%.2f",area);printf("thegirthis%.2f",girth);}elseprintf("Inputerror!");return0;}3、已知函數(shù)y=f(x),編程實現(xiàn)輸入一個x值,輸出y值。2x+1(x<0)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;prin
3、tf(“%d”,y);}4.從鍵盤上輸入一個百分制成績score,按下列原則輸出其等級:score≥90,等級為A;80≤score<90,等級為B;70≤score<80,等級為C;60≤score<70,等級為D;score<60,等級為E。#includevoidmain(){intdata;chargrade;printf("Pleaseenterthescore:");scanf("%d”,&data);switch(data/10){case10:case9:grade=’A’;break;case8:grade=’B’;break;case7:grade=
4、’C’;break;case6:grade=’D’;break;default:grade=’E’;}printf("thegradeis%c”,grade);}5.編一程序每個月根據(jù)每個月上網(wǎng)時間計算上網(wǎng)費用,計算方法如下:要求當輸入每月上網(wǎng)小時數(shù),顯示該月總的上網(wǎng)費用(6分)#includevoidmain(){inthour;floatfee;printf(“pleaseinputhour:”);scanf(“%d”,&hour);if(hour<=10)fee=30;elseif(hour>=10&&hour<=50)fee=3*hour;elsefee=h
5、our*2.5;printf(“Thetotalfeeis%f”,fee);}6.從鍵盤輸入10個整數(shù),統(tǒng)計其中正數(shù)、負數(shù)和零的個數(shù),并在屏幕上輸出。#includevoidmain(){inta,i,p=0,n=0,z=0;printf("pleaseinputnumber");for(i=0;i<10;i++){scanf("%d,",&a);if(a>0)p++;elseif(a<0)n++;elsez++;}printf("正數(shù):%5d,負數(shù):%5d,零:%5d",p,n,z);}7、編程序實現(xiàn)求1-10之間的所有數(shù)的乘積并輸出。#include6、.h>voidmain(){inti;longsum=1;for(i=1;i<=10;i=i+1)sum=sum*i;printf(“thesumofoddis:%ld”,sum);}8.從鍵盤上輸入10個數(shù),求其平均值。#includevoidmain(){inta,i,sum=0;floatave;;for(i=0;i<10;i++){scanf("%d",&a);sum+=a;}ave=(float)sum/10;printf("ave=%f",ave);}9、編程序實現(xiàn)求1-1000之間的所有奇數(shù)的和并輸出。#includevoidmain
7、(){inti,sum=0;for(i=1;i<1000;i=i+2)sum=sum+i;printf(“thesumofoddis:%d”,sum);}10.有一個分數(shù)序列:2/1,3/2,5/3,8/5,13/8,……編程求這個序列的前20項之和。#includevoidmain(){inti,t,n=20;floata=2,b=1,s=0;for(i=1;i<=n;i++){s=s+a/b;t=a;a=a+b