Applying Discounts – Only 1 Volume Slab – Computer Sir Ki Class Code Learning #CPP#1878    ## Applying Discounts – Only 1 Volume Slab

Applying discounts on price based on only 1 quantity or volume of purchase based slab.

## Learning Objectives

• Learning to handle one slab of discount with a simple if-else.
• Learning multiple statements in both if and else conditions, thus using the scope { } braces properly.
• Learning to give instructions at the right place, inside or outside of the selection construct.

## Source Code

 #include using namespace std; int main() { float price,amount2pay; int items,discount; cout<<"Enter item price : ";cin>>price; cout<<"Enter number of items purchased : "; cin>>items; if(items>=10) { discount=20; cout<<"Based on your purchase volume " "you get a special discount"<

## Run Output

``````Enter item price : 100
Enter number of items purchased : 20
Based on your purchase volume you get a special discount
Amount to pay = 1600

- OR -
Enter item price : 100
Enter number of items purchased : 5
You get a regular discount
Amount to pay = 450``````

## Code Understanding

float price,amount2pay;
These are currency related fields so they have been declared as float values.

int items,discount;
these are item quantity and discount related fields so they are taken as integer values. assumption is that items can not be partial and discount percentages are also whole numbers.

cout<<“Enter item price : “;cin>>price;
cout<<“Enter number of items purchased : “; cin>>items;
Item price and item puchase quantity are collected from the user.

if(items>=10) { discount=20; cout<<“Based on your purchase volume ” “you get a special discount”<<endl; }
Here we decide to give higher discount when the slab of purchase is equal to or higher than 10.

else {discount=10;cout<<“You get a regular discount”<<endl;}
When purchase is below the slab we give the regular discount.

Here we print the discount outside the selection statement as fixed within the true or false condition of the if-else selection.

amount2pay=items*(price-price*discount/100);
Here we calculate the final amout to pay. In this formula we assume that the discount is in percentage so we divide it by hundred. Then we calculate the discounted price of 1 item by subtracting the discount from the original price. Finally we multiply it by number of items to get the final amount to pay.

cout<<“Amount to pay = “<<amount2pay<<endl;
We finally print the calculated amount to be paid by the customer.

## Notes

• The discount value could have been printed inside the selection construct also separately for if block and the else block. But this would be duplication of effort. So it is better to do it after the whole if and else block has finished. Just fixing the right discount amount inside the if-else construct is the correct approach.

Suggested Filename(s): discount.cpp, discount1slab.cpp

sunmitra| Created: 23-Dec-2017 | Updated: 15-Sep-2018|

×