C语言-全部题目及其答案.doc
《C语言-全部题目及其答案.doc》由会员分享,可在线阅读,更多相关《C语言-全部题目及其答案.doc(19页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、C C 语言全部题目及答案语言全部题目及答案Exercise 1: Programming Environment and Basic Input/Output1. 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 this program to print “T
2、his is my 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 speci
3、fier (i.e. 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:#incl
4、udeint main() printf(“This is my first program!“);return 0; #includeint main() printf(“This is my second program!“);return 0; #includeint main() printf(“1“); printf(“2“); printf(“3“); printf(“4“);return 0; #includeint main() printf(“1234“);return 0;#includeint main() float days,minutes; days = 15; m
5、inutes = days * 24 * 60; printf(“The number of minutes in 15 days are %fn“, minutes);return 0; #includeint main() float minutes,hours; minutes = 180;#includeint main() printf(“%d%d%d%d“,1,2,3,4);return 0; hours = minutes / 60; printf(“180 minutes equal to %f hoursn“, hours);return 0; #includeint mai
6、n() float minutes,hours; minutes = 174; hours = minutes / 60; printf(“174 minutes equal to %f hoursn“, hours);return 0; Exercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displ
7、ays the total purchase price (net price + sales tax).2Write a program that reads in the radius of a circle and prints the circles diameter, circumference and area. Use the value 3.14159 for “”.3Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a p
8、ercentage. 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 run:Interest calculation program. Starting balance? 6000 Annual interest rate percentage? 4.25 Balance
9、 after one year: 6255ANSWER: #includeint main() float net_price,sales_tax,total; net_price = 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,diame
10、ter,circumference,area; scanf(“%f“, printf(“The diameter is %gn“,diameter = radius * 2); 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 calcula
11、tion programnnPlease enter the Starting Balance:“); scanf(“%f“, printf(“Please enter the Annual interest rate percentage:“); scanf(“%f“, NB = SB * percentage / 100 + SB; printf(“nThe Balance after one year is:%g“,NB);return 0; Exercise 3: Selection structure1Write a C program that accepts a students
12、 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 an odd or even number. 3Write a pr
13、ogram that reads in three integers and then determines and prints the largest in the group.ANSWER: #include#includeint main() int a;printf(“Please enter an integer numbern“);printf(“Then Ill tell you whether its an odd or even number“);#includeint main() int grade;printf(“Please enter the grade:“);s
14、canf(“%d“,if (grade = 60 printf(“Please enter 3 integer numbersn“);printf(“Then Ill tell you which is the largestn“);scanf(“%d%d%d“,if (a b else if (b a else if (c a elseprintf(“Theyre equal“);return 0; scanf(“%d“,if (a%2 = 0)printf(“%d is an even number“,a);elseprintf(“%d is a odd number“,a);return
15、 0; Exercise 4: switch statement and simple “while” repetition statement1. 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 December 1994. The day should be followed by an appropriate suffix, st, nd, rd or t
16、h. 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. They receive 200 per week plus 9% of their gross sales for that week. For example, s
17、omeone 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 salespersons sales for the previous week, and print out their salary. Process one persons figures at a time.Enter sales in pounds (-1 to end): 5000.00 Salary is: 650
18、.00Enter sales in pounds (-1 to end): 00.00 Salary is: 200.00Enter sales in pounds (-1 to end): 1088.89 Salary is: 298.00Enter sales in pounds (-1 to end): -1Optional: 4.A mail order company sells five different products whose retail prices are shown in the following table: Product NumberRetail Pric
19、e (in pounds) 12.98 24.5039.98 44.49 56.87 Write a C program that reads in a series of pairs of numbers as follows: (1). Product number (2). Quantity sold for one day Your program should use a switch statement to help determine the retail price for each product, and should use a sentinel-controlled
20、loop to calculate the total retail value of all products sold in a given week (7days).ANSWER: #include int main() printf(“Please enter three numbers for date:“);int day,month,year;scanf(“%d %d %d“,if(day31)printf(“Error“);elseswitch (day)case 1:printf(“1st“);break;case 2:printf(“2nd“); break;case 3:
21、printf(“3rd“); break; case 21:printf(“21st“); break;case 22:printf(“22nd“); break;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 int main() float a,b; while (a0 ) printf(“Enter sales in pounds (-1 to en
22、d):“);scanf(“%f“,b=200+a*0.09;if (a=-1)printf(“ “);else printf(“Salary is %.0fn“,b); return 0; Exercise 5: for and do while” repetition statements 1. 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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 全部 题目 及其 答案
限制150内