您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > C语言全部题目及答案
C语言全部题目及答案Exercise1:ProgrammingEnvironmentandBasicInput/Output1.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.no‘%’).(c)Usingone“printf”statementwithfourconversionspecifiers3.(a)Writeaprogramthatcalculatesanddisplaysthenumberofminutesin15days.(b)Writeaprogramthatcalculatesanddisplayshowmanyhours180minutesequalto.(c)(Optional)Howabout174minutesANSWERS:#includeintmain(){printf(Thisismyfirstprogram!);return0;}#includeintmain(){printf(Thisismysecondprogram!);#includeintmain(){printf(1);printf(2);printf(3);printf(4);return0;}#includeintmain(){printf(1234);#includeintmain(){floatdays,minutes;days=15;minutes=days*24*60;printf(Thenumberofminutesin15daysare%f\n,minutes);return0;}#includeintmain(){return0;}return0;}#includeintmain(){printf(%d%d%d%d,1,2,3,4);return0;}floatminutes,hours;minutes=180;hours=minutes/60;printf(180minutesequalto%fhours\n,hours);return0;}#includeintmain(){floatminutes,hours;minutes=174;hours=minutes/60;printf(174minutesequalto%fhours\n,hours);return0;}Exercise2:DataTypesandArithmeticOperations1.Youpurchasealaptopcomputerfor$889.Thesalestaxrateis6percent.WriteandexecuteaCprogramthatcalculatesanddisplaysthetotalpurchaseprice(netprice+salestax).2.Writeaprogramthatreadsintheradiusofacircleandprintsthecircle’sdiameter,circumferenceandarea.Usethevaluefor“”.3.Writeaprogramthatreadsintwonumbers:anaccountbalanceandanannualinterestrateexpressedasapercentage.Yourprogramshouldthendisplaythenewbalanceafterayear.Therearenodepositsorwithdraws–justtheinterestpayment.Yourprogramshouldbeabletoreproducethefollowingsamplerun:Interestcalculationprogram.Startingbalance6000AnnualinterestratepercentageBalanceafteroneyear:6255ANSWER:#includeintmain(){floatnet_price,sales_tax,total;net_price=889;sales_tax=net_price*;total=net_price+sales_tax;printf(Thetotalpurchasepriceis%g,total);return0;}#includeintmain(){printf(Pleaseinputanumberasradius:\n);floatradius,diameter,circumference,area;scanf(%f,&radius);printf(Thediameteris%g\n,diameter=radius*2);printf(Thecircumferenceis%g\n,circumference=radius*2*;printf(Theareais%g\n,area=radius*radius*;return0;}#includeintmain(){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:Selectionstructure1.WriteaCprogramthatacceptsastudent’snumericalgrade,convertsthenumericalgradetoPassed(gradeisbetween60-100),Failed(gradeisbetween0-59),orError(gradeislessthan0orgreaterthan100).2.Writeaprogramthataskstheusertoenteranintegernumber,thentellstheuserwhetheritisanoddorevennumber.3.Writeaprogramthatreadsinthreeintegersandthendeterminesandprintsthelargestinthegroup.ANSWER:#include#includeintmain(){intgrade;printf(Pleaseenterthegrade:);scanf(%d,&grade);if(grade=60&&grade=100)printf(Passed.);elseif(grade=0&&grade60)printf(Failed.);elseprintf(Error.);return0;}#includeintmain(){inta,b,c;printf(Pleaseenter3integernumbers\n);printf(ThenI'lltellyouwhichisthelargest\n);scanf(%d%d%d,&a,&b,&c);if(ab&&ac)printf(%disthelargest,a);elseif(ba&&bc)printf(%disthelargest,b);elseif(ca&&cb)printf(%disthelargest,c);elseprintf(They'reequal);return0;}#includeintmain(){inta;printf(Pleaseenteranintegernumber\n);printf(ThenI'lltellyouwhetherit'sanoddorevennumber);scanf(%d,&a);if(a%2==0)printf(%disanevennumber,a);elseprintf(%disaoddnumber,a);return0;}Exercise4:‘switch’statementandsimple“while”repetitionstatement1.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):Salaryis:Entersalesinpounds(-1toend):Salaryis:Entersalesinpounds(-1toend):Salaryis:Entersalesinpounds(-1toend):-1Optional:4.Amailordercompanysellsfivedifferentproductswhoseretailpricesareshowninthefollowingtable:ProductNumberRetailPrice(inpounds)12345WriteaCprogramthatreadsinaseriesofpairsofnumbersasfollows:(1).Productnumber(2).QuantitysoldforonedayYourprogramshoulduseaswitchstatementtohelpdeterminetheretailpriceforeachproduct,andshou
本文标题:C语言全部题目及答案
链接地址:https://www.777doc.com/doc-4907943 .html