2022年C语言全部题目及答案 .pdf
《2022年C语言全部题目及答案 .pdf》由会员分享,可在线阅读,更多相关《2022年C语言全部题目及答案 .pdf(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年C语言全部题目及答案 2022 语言 全部 题目 答案
限制150内