Applying Discounts – Only 1 Volume Slab – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1878 siteicon   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

Run Output

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

- OR -
Enter item price : 100
Enter number of items purchased : 5
You get a regular discount
Your discount is 10%
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.

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

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






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back