C语言全部题目及答案.docx

上传人:b****6 文档编号:8030968 上传时间:2023-01-28 格式:DOCX 页数:22 大小:22.99KB
下载 相关 举报
C语言全部题目及答案.docx_第1页
第1页 / 共22页
C语言全部题目及答案.docx_第2页
第2页 / 共22页
C语言全部题目及答案.docx_第3页
第3页 / 共22页
C语言全部题目及答案.docx_第4页
第4页 / 共22页
C语言全部题目及答案.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

C语言全部题目及答案.docx

《C语言全部题目及答案.docx》由会员分享,可在线阅读,更多相关《C语言全部题目及答案.docx(22页珍藏版)》请在冰豆网上搜索。

C语言全部题目及答案.docx

C语言全部题目及答案

C语言全部题目及答案

Exercise1:

ProgrammingEnvironmentandBasicInput/Output

1.Writeaprogramthatprints“Thisismyfirstprogram!

”onthescreen.

(a)Savethisprogramontoyourowndiskwiththenameofe2-1a;

(b)RunthisprogramwithoutopeningTurboC;

(c)Modifythisprogramtoprint“Thisismysecondprogram!

”,thensaveitase2-1b.Pleasedonotoverwritethefirstprogram.

2.Writeaprogramthatprintsthenumber1to4onthesameline.Writetheprogramusingthefollowingmethods:

(a)Usingfour“printf”statements.

(b)Usingone“printf”statementwithnoconversionspecifier(i.e.no‘%’).

(c)Usingone“printf”statementwithfourconversionspecifiers

3.(a)Writeaprogramthatcalculatesanddisplaysthenumberofminutesin15days.

(b)Writeaprogramthatcalculatesanddisplayshowmanyhours180minutesequalto.

(c)(Optional)Howabout174minutes?

ANSWERS:

#include

intmain()

{

printf("Thisismyfirstprogram!

");

return0;

}

#include

intmain()

{

printf("Thisismysecondprogram!

");

return0;

}

#include

intmain()

{

printf("1");

printf("2");

printf("3");

printf("4");

return0;

}

#include

intmain()

{

printf("1234");

return0;

}

#include

intmain()

{

printf("%d%d%d%d",1,2,3,4);

return0;

}

#include

intmain()

{

floatdays,minutes;

days=15;

minutes=days*24*60;

printf("Thenumberofminutesin15daysare%f\n",minutes);

return0;

}

#include

intmain()

{

floatminutes,hours;

minutes=180;

hours=minutes/60;

printf("180minutesequalto%fhours\n",hours);

return0;

}

#include

intmain()

{

floatminutes,hours;

minutes=174;

hours=minutes/60;

printf("174minutesequalto%fhours\n",hours);

return0;

}

Exercise2:

DataTypesandArithmeticOperations

1.Youpurchasealaptopcomputerfor$889.Thesalestaxrateis6percent.WriteandexecuteaCprogramthatcalculatesanddisplaysthetotalpurchaseprice(netprice+salestax).

2.Writeaprogramthatreadsintheradiusofacircleandprintsthecircle’sdiameter,circumferenceandarea.Usethevalue3.14159for“?

”.

3.Writeaprogramthatreadsintwonumbers:

anaccountbalanceandanannualinterestrateexpressedasapercentage.Yourprogramshouldthendisplaythenewbalanceafterayear.Therearenodepositsorwithdraws–justtheinterestpayment.Yourprogramshouldbeabletoreproducethefollowingsamplerun:

Interestcalculationprogram.

Startingbalance?

6000

Annualinterestratepercentage?

4.25

Balanceafteroneyear:

6255

ANSWER:

#include

intmain()

{

floatnet_price,sales_tax,total;

net_price=889;

sales_tax=net_price*0.06;

total=net_price+sales_tax;

printf("Thetotalpurchasepriceis%g",total);

return0;

}

#include

intmain()

{

printf("Pleaseinputanumberasradius:

\n");

floatradius,diameter,circumference,area;

scanf("%f",&radius);

printf("Thediameteris%g\n",diameter=radius*2);

printf("Thecircumferenceis%g\n",circumference=radius*2*3.14159);

printf("Theareais%g\n",area=radius*radius*3.14159);

return0;

}

#include

intmain()

{

floatSB,percentage,NB;

printf("Interestcalculationprogram\n\nPleaseentertheStartingBalance:

");

scanf("%f",&SB);

printf("PleaseentertheAnnualinterestratepercentage:

");

scanf("%f",&percentage);

NB=SB*percentage/100+SB;

printf("\nTheBalanceafteroneyearis:

%g",NB);

return0;

}

Exercise3:

Selectionstructure

1.WriteaCprogramthatacceptsastudent’snumericalgrade,convertsthenumericalgradetoPassed(gradeisbetween60-100),Failed(gradeisbetween0-59),orError(gradeislessthan0orgreaterthan100).

2.Writeaprogramthataskstheusertoenteranintegernumber,thentellstheuserwhetheritisanoddorevennumber.

3.Writeaprogramthatreadsinthreeintegersandthendeterminesandprintsthelargestinthegroup.

ANSWER:

#include

intmain()

{

intgrade;

printf("Pleaseenterthegrade:

");

scanf("%d",&grade);

if(grade>=60&&grade<=100)

printf("Passed.");

elseif(grade>=0&&grade<60)

printf("Failed.");

else

printf("Error.");

return0;

}

#include

intmain()

{

inta,b,c;

printf("Pleaseenter3integernumbers\n");

printf("ThenI'lltellyouwhichisthelargest\n");

scanf("%d%d%d",&a,&b,&c);

if(a>b&&a>c)

printf("%disthelargest",a);

elseif(b>a&&b>c)

printf("%disthelargest",b);

elseif(c>a&&c>b)

printf("%disthelargest",c);

else

printf("They'reequal");

return0;

}

#include

#include

intmain()

{

inta;

printf("Pleaseenteranintegernumber\n");

printf("ThenI'lltellyouwhetherit'sanoddorevennumber");

scanf("%d",&a);

if(a%2==0)

printf("%disanevennumber",a);

else

printf("%disaoddnumber",a);

return0;

}

Exercise4:

‘switch’statementandsimple“while”repetitionstatement

1.Writeaprogramthatreadsthreeintegersanabbreviateddate(forexample:

261294)andthatwillprintthedateinfull;forexample:

26thDecember1994.Thedayshouldbefollowedbyanappropriatesuffix,‘st’,‘nd’,‘rd’or‘th’.Useatleastoneswitchstatement.

2.WriteaCprogramthatusesawhilelooptocalculateandprintthesumoftheevenintegersfrom2to30.

3.Alargechemicalcompanypaysitssalesstaffonacommissionbasis.Theyreceive£200perweekplus9%oftheirgrosssalesforthatweek.Forexample,someonewhosells£5000ofchemicalsinoneweekwillearn£200plus9%of£5000,atotalof£650.DevelopaCprogramthatwillinputeachsalesperson’ssalesforthepreviousweek,andprintouttheirsalary.Processoneperson’sfiguresatatime.

Entersalesinpounds(-1toend):

5000.00

Salaryis:

650.00

Entersalesinpounds(-1toend):

00.00

Salaryis:

200.00

Entersalesinpounds(-1toend):

1088.89

Salaryis:

298.00

Entersalesinpounds(-1toend):

-1

Optional:

4.Amailordercompanysellsfivedifferentproductswhoseretailpricesareshowninthefollowingtable:

ProductNumberRetailPrice(inpounds)

12.98

24.50

39.98

44.49

56.87

WriteaCprogramthatreadsinaseriesofpairsofnumbersasfollows:

(1).Productnumber

(2).Quantitysoldforoneday

Yourprogramshoulduseaswitchstatementtohelpdeterminetheretailpriceforeachproduct,andshoulduseasentinel-controlledlooptocalculatethetotalretailvalueofallproductssoldinagivenweek(7days).

ANSWER:

#include

intmain()

{

printf("Pleaseenterthreenumbersfordate:

");

intday,month,year;

scanf("%d%d%d",&day,&month,&year);

if(day>31)

printf("Error");

else

{

switch(day)

{

case1:

printf("1st");

break;

case2:

printf("2nd");

break;

case3:

printf("3rd");

break;

case21:

printf("21st");

break;

case22:

printf("22nd");

break;

case23:

printf("23rd");

break;

case31:

printf("31st");

break;

default:

printf("%dth",day);

}

}

switch(month)

{

case1:

printf("January");

break;

case2:

printf("February");

break;

case3:

printf("March");

break;

case4:

printf("April");

break;

case5:

printf("May");

break;

case6:

printf("June");

break;

case7:

printf("July");

break;

case8:

printf("August");

break;

case9:

printf("September");

break;

case10:

printf("October");

break;

case11:

printf("November");

break;

case12:

printf("December");

break;

}

printf("19%d",year);

return0;

}

#include

intmain()

{

inta,b;

a=0;

b=2;

while(b<=30)

{

a=a+b;

b=b+2;

}

printf("Thesumoftheevenintegersfrom2to30is%d",a);

return0;

}

#include

intmain()

{

floata,b;

while(a>0)

{

printf("Entersalesinpounds(-1toend):

");

scanf("%f",&a);

b=200+a*0.09;

if(a==-1)

printf("");

elseprintf("Salaryis%.0f\n",b);

}

return0;

}

Exercise5:

‘for’and‘do…while”repetitionstatements

1.Writeaprogramwhichusesado/whilelooptoprintoutthefirst10powersof2otherthan0(ie.itprintsoutthevaluesof21,22,...,210).Useaforlooptodothesame.

2.Theconstant?

canbecalculatedbytheinfiniteseries:

?

=4-4/3+4/5-4/7+4/9-4/11+....

WriteaCprogramthatusesado/whilelooptocalculate?

usingtheseries.Theprogramshouldasktheuserhowmanytermsintheseriesshouldbeused.Thusiftheuserenters‘3’,thentheprogramshouldcalculate?

asbeing4-4/3+4/5.

Nestedrepetition

3.Writeaprogramthatprintsthefollowingdiamondshape.Youmayuseprintfstatementsthatprinteitherasingleasterisk(*)orasingleblank.Maximizeyouruseofrepetition(withnestedforstatements)andminimizethenumberofprintfstatements.

*

***

*****

*******

*********

*******

*****

***

*

4.Writeaprogramtoprintatableasfollows:

1*1=1

2*1=22*2=4

3*1=33*2=63*3=9

….

9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81

ANSWER:

#include

intmain()

{

inta,b;

a=0;

b=1;

do

{

a++;

b=b*2;

printf("%d",b);

}

while(a<=9);

return0;

}

#include

intmain()

{

inta;

for(a=2;a<=1024;a=a*2)

printf("%d",a);

return0;

}

#include

intmain(){

doublec,pie,p;

inta,b,d,n;

printf("Enterterms:

");

scanf("%d",&a);

printf("Pie=");

n=1;p=0;

while(n<=a)

{

if(n%2==0)

b=-1;

else

b=1;

pie=(4.0*b)/(2.0*n-1.0);

d=2*n-1;

p=p+pie;

if(n>1&&n<=a)

{

if(n%2!

=0)

printf("+");

elseprintf("-");

}

printf("4/%d",d);

n++;

}

printf("\n=%f",p);

return0;

}

#include

intmain(){

introw,a,b,j;

row=1;

j=4;

while(row<=5){

for(a=j;a>=1;a=a-1)

printf("");

for(b=1;b<=9-2*j;b++)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1