Applying Discounts – Only 1 Volume Slab – CSKC
Sign In

Search

Code Learning #CPP#1878    siteicon   siteicon  

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

TC++ #1878

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

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.

cout<<“Your discount is “<<discount<<“%”<<endl;
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



Share

Created: 23-Dec-2017 13:28 | Updated: 15-Sep-2018 11:28|