186k views
0 votes
Write a C function which uses the return statement to calculate and return the total cost for purchasing books(quantity*books cost). 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 are wanted, then each book costs 18.75( NOT A COMPLETE PROGRAM)

User Maged Adel
by
5.4k points

1 Answer

5 votes

Answer:

#include<cstdio>

#include<conio.h>

using namespace std;

float totalcost(int totalbooks);

main()

{

int totalbooks;

float cost;

printf("enter the number of books");

scanf("%d",&totalbooks);

cost = totalcost (totalbooks);

printf("Total Purchasing Cost =%f",cost);

getch();

}

float totalcost(int totalbooks)

{

float cost, costperbook;

if(totalbooks<=20)

{

costperbook = 23.45;

return cost=costperbook*totalbooks;

}

else

if(totalbooks>=20 && totalbooks<=100)

{

costperbook = 21.11;

return cost=costperbook*totalbooks;

}

else

costperbook = 18.75;

return cost=costperbook*totalbooks;

}

Step-by-step explanation:

In this program a function has been used to calculate the total cost of the book. There is different cost for different number of books. To accomplish the task, two variable taken as float data type to store the total cost because cost of each book is assigned in decimal value. Total cost is the multiplication of cost per book and total number of books. As there is different cost for different number of books, so I use conditional statements to meet the demand of the cost per book.

User Balzard
by
6.3k points