08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ Programs with Selection Logic

Exercises 4

1. An auto repair shop charges as follows. Inspecting the vehicle costs $75. If no work

needs to be done, there is no further charge. Otherwise, the charge is $75 per hour

for labor plus the cost of parts, with a minimum charge of $120. If any work is

done, there is no charge for inspecting the vehicle.

Write a program to read values for hours worked and cost of parts (either of which

could be 0) and print the charge for the job.

2. Write a program that requests two weights in kilograms and grams and prints the

sum of the weights. For example, if the weights are 3kg 500g and 4kg 700g,

your program should print 8kg 200g.

3. Write a program that requests two lengths in feet and inches and prints the sum

of the lengths. For example, if the lengths are 5 ft. 4 in. and 8 ft. 11 in.,

your program should print 14 ft. 3 in. (1 ft. = 12 in.)

4. A variety store gives a 15% discount on sales totaling $300 or more. Write a

program to request the cost of three items and print the amount the customer

must pay.

5. Write a program to read two pairs of integers. Each pair represents a fraction. For

example, the pair 3 5 represents the fraction 3/5. Your program should print the

sum of the given fractions. For example, give the pairs 3 5 and 2 3, your program

should print 19/15, since 3/5 + 2/3 = 19/15.

Modify the program so that it prints the sum with the fraction reduced to a proper

fraction; for this example, your program should print 1 4/15.

6. Write a program to read a person’s name, hours worked, hourly rate of pay, and tax

rate (a number representing a percentage, e.g., 25 meaning 25%). The program

must print the name, gross pay, tax deducted, and gross pay.

Gross pay is calculated as described in Section 4.4.1. The tax deducted is calculated by

applying the tax rate to 80% of gross pay. And the net pay is calculated by subtracting

the tax deducted from the gross pay.

For example, if the person works 50 hours at $20/hour and the tax rate is 25%, his

gross pay would be (40 x 20) + (10 20 1.5) = $1100. He pays 25% tax on 80% of

$1100, that is, 25% of $880 = $220. His net pay is 1100 - 220 = $880.

7. Write a program to read integer values for month and year and print the number

of days in the month. For example, 4 2005 (April 2005) should print 30, 2 2004

(February 2004) should print 29 and 2 1900 (February 1900) should print 28.

A leap year, n, is divisible by 4; however, if n is divisible by 100 then it is a leap year

only if it is also divisible by 400. So 1900 is not a leap year but 2000 is.

87

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!