5.4k views
3 votes
Write a C function (NOT A COMPLETE PROGRAM) which uses the RETURNstatement to calculate and return the total cost for purchasing books (quantity * bookcost). The parameter needed is the quantity of books wanted. If 20 or fewer books are wanted, then each book costs $23.45. If 21 to 100 books are wanted, then each book costs $21.11. If more than 100 books arewanted, then each book costs $18.75. Please assume that number of books passed to your function is positive.

1 Answer

3 votes

Answer:

int calculate_cost(int quantity) {

double cost = 0;

if (quantity <= 20)

cost = quantity * 23.45;

else if (quantity >= 21 && quantity <= 100)

cost = quantity * 21.11;

else if (quantity > 100)

cost = quantity * 18.75;

return cost;

}

Step-by-step explanation:

Create a function called calculate_cost that takes one parameter, quantity

Initialize the cost as 0

Check the quantity using if else structure. Depending on the quantity passed, calculate the cost. For example, if the quantity is 10, the cost will be $234.5

Return the cost

User Fourk
by
5.3k points