In: Computer Science
=========================================================================
A software company sells a package that retails for $99.
Quantity discounts are given
according to the following table.
Quantity Discount
10–19 20%
20–49 30%
50–99 40%
100 or more 50%
Write a program that asks for the number of units sold and computes
the total cost of
the purchase.
Input Validation: Make sure the number of units is greater than
0.
=========================================================================
the pseudo code is:
There comes situations in real life when we need to make some
decisions and based on these decisions, we decide what should we do
next. Similar situations arises in programming also where we need
to make some decisions and based on these decision we will execute
the next block of code. Decision making statements in programming
languages decides the direction of flow of program execution.
Decision making structures require that the programmer specify one
or more conditions to be evaluated or tested by the program
selection construct, along with a statement or statements to be
executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to
be false.
When a customer purchases some quantity of our software package, we
need to make a decision about how much discount to apply to the
purchase. Our program will need to evaluate at least 4 to 5
conditions to determine which discount to apply.
We will use a decision making structure that can evaluate at test for five(5) possible conditions along with a statement to apply the discount if the condition is determined to be true.
Since we have multiple conditions to evaluate, we shall select to implement the if-else-if ladder construct which has the following syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
=========================================================================
declare const for price
double const PRICE(99.00);
declare variables
int qty, double totalCost, discount
prompt user for qty
validate data & determine discount
if( qty <= 0 ) cout<<"Invalid Value\n"
else if( qty < 20 ) discount = 0.20
.
.
else discount = 0.50
compute totalCost
display results
----------------------------------------------------------------------------
Code
#include<iostream>
using namespace std;
int main()
{
double const PRICE=99.00;
int qty;
double totalCost, discount=0.0;
cout<<"How many computer product sold: ";
cin>>qty;
if( qty <= 0 )
{
cout<<"Invalid Value\n";
return 0;
}
else if( qty < 20 ) discount = 0.20;
else if(qty<50) discount=0.30;
else if(qty<100) discount=0.40;
else discount=0.50;
totalCost=(qty*PRICE)-((qty*PRICE)*discount);
cout<<"Total cost is : $"<<totalCost<<endl;
return 1;
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.