資源描述:
《c語(yǔ)言練習(xí)題帶詳解答案》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、一、編程題1.輸入2個(gè)整數(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.輸入一個(gè)圓半徑r,當(dāng)r>=0時(shí),計(jì)算并輸出圓的面積和周長(zhǎng),否則,輸出提示信息。#include#definePI3.14intmain(void){doubler,area,girth;printf("pleaseinp
2、utr:");scanf("%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),編程實(shí)現(xiàn)輸入一個(gè)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;e
3、lseif(x>0)y=2*x-1;elsey=0;printf(“%d”,y);}4.從鍵盤(pán)上輸入一個(gè)百分制成績(jī)score,按下列原則輸出其等級(jí):score≥90,等級(jí)為A;80≤score<90,等級(jí)為B;70≤score<80,等級(jí)為C;60≤score<70,等級(jí)為D;score<60,等級(jí)為E。#includevoidmain(){intdata;chargrade;printf("Pleaseenterthescore:");scanf("%d”,&data);switch(data/10){case10:case9:grade
4、=’A’;break;case8:grade=’B’;break;case7:grade=’C’;break;case6:grade=’D’;break;default:grade=’E’;}printf("thegradeis%c”,grade);}5.編一程序每個(gè)月根據(jù)每個(gè)月上網(wǎng)時(shí)間計(jì)算上網(wǎng)費(fèi)用,計(jì)算方法如下:要求當(dāng)輸入每月上網(wǎng)小時(shí)數(shù),顯示該月總的上網(wǎng)費(fèi)用(6分)#includevoidmain(){inthour;floatfee;printf(“pleaseinputhour:”);scanf(“%d”,&hour);if(ho
5、ur<=10)fee=30;elseif(hour>=10&&hour<=50)fee=3*hour;elsefee=hour*2.5;printf(“Thetotalfeeis%f”,fee);}6.從鍵盤(pán)輸入10個(gè)整數(shù),統(tǒng)計(jì)其中正數(shù)、負(fù)數(shù)和零的個(gè)數(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++;}pri
6、ntf("正數(shù):%5d,負(fù)數(shù):%5d,零:%5d",p,n,z);}7、編程序?qū)崿F(xiàn)求1-10之間的所有數(shù)的乘積并輸出。#includevoidmain(){inti;longsum=1;for(i=1;i<=10;i=i+1)sum=sum*i;printf(“thesumofoddis:%ld”,sum);}8.從鍵盤(pán)上輸入10個(gè)數(shù),求其平均值。#includevoidmain(){inta,i,sum=0;floatave;;for(i=0;i<10;i++){scanf("%d",&a);sum+=a;}ave=
7、(float)sum/10;printf("ave=%f",ave);}9、編程序?qū)崿F(xiàn)求1-1000之間的所有奇數(shù)的和并輸出。#includevoidmain(){inti,sum=0;for(i=1;i<1000;i=i+2)sum=sum+i;printf(“thesumofoddis:%d”,sum);}10.有一個(gè)分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,……編程求這個(gè)序列的前20項(xiàng)之和。#includevoidmain(){inti,t,n=20;floata=2,b=1,s=0;for(i=1;i
8、<=n;i++){s=s+a/b;t=a;a=a+b