《2022年C语言全部题目及答案 .pdf》由会员分享,可在线阅读,更多相关《2022年C语言全部题目及答案 .pdf(19页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C语言全部题目及答案Exercise 1: Programming Environment and Basic Input/Output 1. Write a program that prints “This is my first program!” on the screen. (a) Save this program onto your own disk with the name of e2-1a; (b) Run this program without opening Turbo C; (c) Modify th is program to print “This is my
2、second program!”, then save it as e2-1b. Please do not overwrite the first program. 2. Write a program that prints the number 1 to 4 on the same line. Write the program using the following methods: (a) Using four “printf” statements. (b) Using one “printf” statement with no conversion specifier(i.e.
3、 no ,%?).(c) Using one “printf” statement with four conversion specifiers3(a) Write a program that calculates and displays the number of minutes in 15 days. (b) Write a program that calculates and displays how many hours 180 minutes equal to. (c) (Optional) How about 174 minutes? ANSWERS: #include i
4、nt main() printf(This is my first program!); return 0; #include int main() printf(This is my second program!); return 0; #include int main() printf(1); printf(2); printf(3); printf(4); return 0; #include int main() printf(1234); return 0; #include int main() float days,minutes; days = 15; minutes =
5、days * 24 * 60; printf(The number of minutes in 15 days are %fn, minutes); return 0; #include int main() float minutes,hours; minutes = 180; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 19 页 - - - - - - - - - #include int main() printf(%d%d%d%d,1,2,3,4); retu
6、rn 0; hours = minutes / 60; printf(180 minutes equal to %f hoursn, hours); return 0; #include int main() float minutes,hours; minutes = 174; hours = minutes / 60; printf(174 minutes equal to %f hoursn, hours); return 0; Exercise 2: Data Types and Arithmetic Operations 1. You purchase a laptop comput
7、er for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displays the total purchase price (net price + sales tax). 2Write a program that reads in the radius of a circle and prints the circle?s diameter, circumference and area. Use the value 3.14159 for “”.3Wri
8、te a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then display the new balance after a year. There are no deposits or withdraws just the interest payment. Your program should be able to reproduce the following sample
9、 run: Interest calculation program. Starting balance? 6000Annual interest rate percentage? 4.25Balance after one year: 6255名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 19 页 - - - - - - - - - ANSWER: #include int main() float net_price,sales_tax,total; net_pri
10、ce = 889; sales_tax = net_price * 0.06; total = net_price + sales_tax; printf(The total purchase price is %g, total); return 0; #include int main() printf(Please input a number as radius:n); float radius,diameter,circumference,area; scanf(%f,&radius); printf(The diameter is %gn,diameter = radius * 2
11、); printf(The circumference is %gn,circumference = radius * 2 * 3.14159); printf(The area is %gn, area = radius * radius * 3.14159); return 0; #include int main() float SB,percentage,NB; printf(Interest calculation programnnPlease enter the Starting Balance:); scanf(%f,&SB); printf(Please enter the
12、Annual interest rate percentage:); scanf(%f,&percentage); NB = SB * percentage / 100 + SB; printf(nThe Balance after one year is:%g,NB); return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 19 页 - - - - - - - - - Exercise 3: Selection structure 1Write a C p
13、rogram that accepts a student? s numerical grade, converts the numerical grade to Passed (grade is between 60-100), Failed (grade is between 0-59), or Error (grade is less than 0 or greater than 100). 2Write a program that asks the user to enter an integer number, then tells the user whether it is a
14、n odd or even number. 3Write a program that reads in three integers and then determines and prints the largest in the group.ANSWER: #include #include int main() int a; printf(Please enter an integer numbern); printf(Then Ill tell you whether its an odd or even number); #include int main() int grade;
15、 printf(Please enter the grade:); scanf(%d,&grade); if (grade = 60 & grade = 0 & grade 60) printf(Failed.); else printf(Error.); return 0; #include int main() int a,b,c; printf(Please enter 3 integer numbersn); printf(Then Ill tell you which is the largestn); scanf(%d%d%d,&a,&b,&c); if (a b & a c) p
16、rintf(%d is the largest,a); else if (b a & b c) printf(%d is the largest,b); else if (c a & c b) printf(%d is the largest,c); else printf(Theyre equal); return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 19 页 - - - - - - - - - scanf(%d,&a); if (a%2 = 0) p
17、rintf(%d is an even number,a); else printf(%d is a odd number,a); return 0; Exercise 4: ,switch? statement and simple “ while” repetition statement 1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) and that will print the date in full; for example: 26th Decembe
18、r 1994. The day should be followed by an appropriate suffix, ,st?, ,nd?, ,rd? or ,th?. Use at least one switch statement. 2Write a C program that uses a while loop to calculate and print the sum of the even integers from 2 to 30. 3. A large chemical company pays its sales staff on a commission basis
19、. They receive 200 per week plus 9% of their gross sales for that week. For example, someone who sells 5000 of chemicals in one week will earn 200 plus 9% of 5000, a total of 650. Develop a C program that will input each salesperson?s sales for the previous week, and print out their salary. Process
20、one person?s figures at a time.Enter sales in pounds (-1 to end): 5000.00 Salary is: 650.00 Enter sales in pounds (-1 to end): 00.00 Salary is: 200.00 Enter sales in pounds (-1 to end): 1088.89 Salary is: 298.00 Enter sales in pounds (-1 to end): -1 Optional :4. A mail order company sells five diffe
21、rent products whose retail prices are shown in the following table: Product Number Retail Price (in pounds) 1 2.98 2 4.50 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 19 页 - - - - - - - - - 3 9.98 4 4.49 5 6.87 Write a C program that reads in a series of pair
22、s of numbers as follows: (1). Product number (2). Quantity sold for one day Your program should use a switchstatement to help determine the retail price for each product, and should use a sentinel-controlled loop to calculate the total retail value of all products sold in a given week (7days). ANSWE
23、R: #include int main() printf(Please enter three numbers for date:); int day,month,year; scanf(%d %d %d,&day,&month,&year); if(day31) printf(Error); else switch (day) case 1:printf(1st); break; case 2:printf(2nd); break; case 3:printf(3rd); break; case 21:printf(21st); break; case 22:printf(22nd); b
24、reak; case 23:printf(23rd); break; case 31:printf(31st); break; default:printf(%dth,day); switch(month) #include int main() int a,b; a=0; b=2; while (b=30) a=a+b; b=b+2; printf(The sum of the even integers from 2 to 30 is %d,a); return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心
25、整理 - - - - - - - 第 6 页,共 19 页 - - - - - - - - - case 1: printf(January ); break; case 2: printf(February ); break; case 3: printf(March ); break; case 4: printf(April ); break; case 5: printf(May ); break; case 6: printf(June ); break; case 7: printf(July ); break; case 8: printf(August ); break; ca
26、se 9: printf(September ); break; case 10: printf(October ); break; case 11: printf(November ); break; case 12: printf(December ); break; printf(19%d,year); return 0; #include int main() float a,b; while (a0 ) printf(Enter sales in pounds (-1 to end):); scanf(%f,&a); b=200+a*0.09; if (a=-1) printf( )
27、; else printf(Salary is %.0fn,b); return 0; Exercise 5: ,for? and ,do while” repetition statements1. Write a program which uses a do/while loop to print out the first 10 powers of 2 other than 0 (ie. it prints out the values of 21, 22, ., 210). Use a for loop to do the same. 2. The constant can be c
28、alculated by the infinite series: = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +. Write a C program that uses a do/while loop to calculate using the series. The program should ask the user how many terms in the series should be used. Thus if the user enters ,3?, then the program should calculate as being 4 -
29、4/3 + 4/5. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 19 页 - - - - - - - - - Nested repetition3. Write a program that prints the following diamond shape. You may use printfstatements that print either a single asterisk (*) or a single blank. Maximize your u
30、se of repetition (with nested for statements) and minimize the number of printfstatements. * * * * * * * * * 4. Write a program to print a table as follows: 1*1= 1 2*1= 2 2*2= 4 3*1= 3 3*2= 6 3*3= 9 . 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 ANSWER: #include int main() int a,b;
31、 a=0; b=1; do a+; b=b*2; printf(%d,b); while (a=9); return 0; #include int main() #include int main() double c,pie,p; int a,b,d,n; printf(Enter terms:); 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; #include int main() int row,a,
32、b,j; row=1; j=4; while(row=1;a=a-1) printf( ); for(b=1;b=1;a=a-1) printf( ); printf(n); row+; j-; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 19 页 - - - - - - - - - int a; for(a=2;a1&n=a) if(n%2!=0) printf(+); else printf(-); printf(4/%d,d); n+; printf(n=%f,
33、p); return 0; row=1; j=1; while(row=5) for(a=1;a=j;a=a+1) printf( ); for(b=1;b=9-2*j;b+) printf(*); for(a=1;a=j;a=a+1) printf( ); printf(n); row+; j+; return 0; #include int main () int i, j; for (i=1; i=9; i+) for (j=1; j=j) printf(%1d*%1d=%2d , i, j, i*j); printf(n); getchar(); return 0; Exercise
34、6: Simple Functions1. Write a C program that reads several numbers and uses the function round_to_nearest to round each of these numbers to the nearest integer. The program should print both the original number and the rounded number. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 -
35、 - - - - - - 第 9 页,共 19 页 - - - - - - - - - 2. Write a program that reads three pairs of numbers and adds the larger of the first pair, the larger of the second pair and the larger of the third pair. Use a function to return the larger of each pair. 3. A car park charges a 2.00 minimum fee to park f
36、or up to 3 hours, and an additional 0.50 for each hour or part hour in excess of three hours. The maximum charge for any given 24-hour period is 10.00. Assume that no car parks for more than 24 hours at a time. Write a C program that will calculate and print the parking charges for each of 3 custome
37、rs who parked their car in the car park yesterday. The program should accept as input the number of hours that each customer has parked, and output the results in a neat tabular form, along with the total receipts from the three customers: Car Hours Charge 1 1.5 2.00 2 4.0 2.50 3 24.0 10.00 TOTAL 29
38、.5 14.50 The program should use the function calculate_charges to determine the charge for each customer. ANSWER: #include #include int main() void round_to_nearest(float); float num; while (5) printf(Please input a number:); scanf(%f,&num); round_to_nearest(num); return 0; #include int main() float
39、 larger_Number(float,float); float num1,num2,total=0; int a=0; while (a=0.5) near_integer=ceil(num1); else near_integer=floor(num1); printf(nThe nearest integer of the number is:%dn,near_integer); return 0; float larger_Number(float num1,float num2) float larger; if (num1=num2) printf(%f,num1); larg
40、er=num1; else printf(%f,num2); larger=num2; printf(n); return (larger); #include int main() float calculate_charges(float); float hour1,hour2,hour3,charge1,charge2,charge3,total1=0,total2=0; printf(Please input three cars parking hours:); scanf(%f%f%f,&hour1,&hour2,&hour3); charge1=calculate_charges
41、(hour1); charge2=calculate_charges(hour2); charge3=calculate_charges(hour3); printf(Car Hours Chargen); printf(1%10.1f%10.2fn,hour1,charge1); printf(2%10.1f%10.2fn,hour2,charge2); printf(3%10.1f%10.2fn,hour3,charge3); total1=hour1+hour2+hour3; total2=charge1+charge2+charge3; printf(TOTAL%7.2f%9.2f,t
42、otal1,total2); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 19 页 - - - - - - - - - return 0; float calculate_charges(float hour) float charge; if (hour3 & hour19 & hour=24) charge=10; return (charge); Exercise 7: More Functions1.Write a program that uses sen
43、tinel-controlled repetition to take an integer as input, and passes it to a function even which uses the modulus operator to determine if the integer is even. The function even should return 1 if the integer is even, and 0 if it is not. The program should take the value returned by the function even
44、 and use it to print out a message announcing whether or not the integer was even. 2. Write a C program that uses the function integerPower1(base, exponent)to return the value of: baseexponentso that, for example, integerPower1(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive,
45、 non-zero integer, and base is an integer. The function should use a forloop, and make no calls to any math library functions. 3. Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of: baseexponent名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - -
46、- - 名师精心整理 - - - - - - - 第 12 页,共 19 页 - - - - - - - - - so that, for example, integerPower2(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponentis a positive, non-zero integer, and baseis an integer. The function should make no calls to any math library functions. (Hint: the recursive step wil
47、l use the relationship: baseexponent = base . baseexponent - 1and the base case will be when exponent is 1 since : base1 = base.) ANSWER: #include int main() int a,b; int judge(int); void judge1(int); printf(Please enter a number); scanf(%d,&a); while(a=-1) b=judge(a); judge1(b); printf(Please evter
48、 a number); scanf(%d,&a); return 0; int judge(int x) if (x%2!=0) return (0); else return (1); void judge1(int x) if (x=1) printf(Its evenn); else printf(Its oddn); #include int main() int integerPower2(int,int); int base,exponent,result; printf(This program can calculate the powern); printf(Enter a
49、integer base number:n); scanf(%d,&base); printf(Enter a non-zero integer number as the exponent:n); scanf(%d,&exponent); result=integerPower2(base,exponent); printf(The power is:%d,result); return 0; int integerPower2(int x,int y) if(y=1) return (x); else return (x*integerPower2(x,y-1); #include int
50、 main() int integerPower1(int,int); int base,exponent,answer; printf(Let us calculate the powern); printf(Please enter a integer base number:n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 19 页 - - - - - - - - - scanf(%d,&base); printf(Please enter a non-ze