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 this program to print “This 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 specifier (i.e. no ?%?). (c) Using one “printf” statement with four conversion specifiers
3.(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
Exercise 2: Data Types and Arithmetic Operations
1. You purchase a laptop computer 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).
2.Write 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 “?”.
3.Write 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 run:
Interest calculation program. Starting balance? 6000
Annual interest rate percentage? 4.25 Balance after one year: 6255
ANSWER:
#include
int main() {
float net_price,sales_tax,total; net_price = 889;
sales_tax = net_price * 0.06; total = net_price + sales_tax;
printf(\
return 0; }
#include
printf(\float radius,diameter,circumference,area; scanf(\
printf(\
printf(\printf(\
return 0; }
#include
float SB,percentage,NB;
printf(\scanf(\
printf(\scanf(\
NB = SB * percentage / 100 + SB;
printf(\
return 0; }
Exercise 3: Selection structure
1.
2. 3.
Write a C program 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).
Write a program that asks the user to enter an integer number, then tells the user whether it is an odd or even number.
Write 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(\
printf(\
scanf(\ if (a%2 == 0)
printf(\ else
printf(\ 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 December 1994. The day should be followed by an appropriate suffix, ?st?, ?nd?, ?rd? or ?th?. Use at least one switch statement.
2.Write 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, 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 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 different products whose retail prices are shown in the
following table:
Product Number Retail Price (in pounds) 1 2.98 2 4.50