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 3 ■ Programs with Sequence Logic

5. Assuming that 12 and 5 are entered as data, identify the logic error in the following

statements (a, b, c, d, and e are int):

scanf("%d %d", &a, &b);

c = (a - b) * 2;

d = e + a;

e = a / (b + 1);

printf("%d %d %d\n", c, d, e);

When the error is corrected, what is printed?

6. What is printed by the following (a, b, and c are int)?

a = 13;

b = a + 12;

printf("%d %d\n", a, b);

c = a + b;

a = a + 11;

printf("%d %d %d\n", a, b, c);

7. Write a program that requests a price and a discount percent. The program prints

the original price, the discount amount, and the amount the customer must pay.

8. Same as 7, but assume that 15% tax must be added to the amount the customer

must pay.

9. Write a program to calculate electricity charges for a customer. The program

requests a name, previous meter reading, and current meter reading. The difference

in the two readings gives the number of units of electricity used. The customer pays

a fixed charge of $25 plus 20 cents for each unit used.

Print all the data, the number of units used, and the amount the customer must pay,

appropriately labeled.

10. Modify 9 so that the program requests the fixed charge and the rate per unit.

11. Write a program to request a student’s name and marks in four subjects. The

program must print the name, total marks, and average mark, appropriately

labeled.

12. Write a program that requests a person’s gross salary, deductions allowed and rate

of tax (e.g., 25, meaning 25%), and calculates his net pay as follows:

Tax is calculated by applying the rate of tax to the gross salary minus the

deductions.

Net pay is calculated by gross salary minus tax.

Print the gross salary, tax deducted, and net pay, appropriately labeled.

Also print the percentage of the gross salary that was paid in tax.

Make up appropriate sets of data for testing the program.

62

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!