codescracker
c++

C++ Program to Calculate Price after applying the Discount



« C++ Tutorial C++ Example »

This article contains a program in C++ that will calculate and print the price to be paid, after applying the discount (if any) based on the total purchase amount done/entered by user (at run-time). The discount must be offered based on the total shopping amount as shown in the table given below:

Total Shopping Amount Discount
<=100 No discount
>100 & <=200 5%
>200 & <=400 10%
>400 & <=800 20%
>800 25%

Find and Print Price to be Paid after applying Discount

The question is, write a program in C++ to find and print the price that has to be paid after applying the discount. The program given below is the answer to this question:

#include<iostream>

using namespace std;
int main()
{
   float amount, discount, topaid;
   cout<<"Enter Total Amount: ";
   cin>>amount;
   if(amount<=100)
      cout<<"Amount to be Paid: "<<amount;
   else
   {
      if(amount>100 && amount<=200)
      {
         discount = (amount*5)/100;
         topaid = amount-discount;
      }
      else if(amount>200 && amount<=400)
      {
         discount = (amount*10)/100;
         topaid = amount-discount;
      }
      else if(amount>400 && amount<=800)
      {
         discount = (amount*20)/100;
         topaid = amount-discount;
      }
      else
      {
         discount = (amount*25)/100;
         topaid = amount-discount;
      }
      cout<<"Amount to be Paid: "<<topaid;
   }
   cout<<endl;
   return 0;
}

The snapshot given below shows the initial output produced by above C++ program on calculating price (amount to be paid) after subtracting the discount (if any) money:

c++ program calculate price after discount

Now enter the total purchase amount say 560 and press ENTER key to calculate and print the price that has to be paid after applying the discount like shown in the snapshot given below:

c++ program calculate price to paid after discount

In above program, I've applied the condition amount<=100 using if that checks whether the purchase amount is less than or equal to 100 or not. If the purchase amount is 100 or less, then there is no discount to apply. Therefore, I've printed as it is, the amount of shopping, customer made.

Otherwise, if the condition evaluates to be False, therefore program flow goes to else's body, and inside the else, I've created total of three conditions, based on the discount given in the table earlier of this article, to check and apply the discount. In this way, the program is created.

Let's modify the above program to provide a good user experience by displaying the discount (if any) applied to his/her purchase:

#include<iostream>

using namespace std;
int main()
{
   float amount, discount, topaid;
   cout<<"Enter Shopping Amount: ";
   cin>>amount;
   if(amount<=100)
   {
      cout<<"\nSorry! No discount for shopping under 100";
      cout<<"You've to Pay "<<amount;
   }
   else
   {
      if(amount>100 && amount<=200)
      {
         cout<<"\nCongrats! You'll get 5% discount on total Shopping amount";
         discount = (amount*5)/100;
         topaid = amount-discount;
      }
      else if(amount>200 && amount<=400)
      {
         cout<<"\nCongrats! You'll get 10% discount on total Shopping amount";
         discount = (amount*10)/100;
         topaid = amount-discount;
      }
      else if(amount>400 && amount<=800)
      {
         cout<<"\nCongrats! You'll get 20% discount on total Shopping amount";
         discount = (amount*20)/100;
         topaid = amount-discount;
      }
      else
      {
         cout<<"\nCongrats! You'll get maximum (25%) discount on total Shopping amount";
         discount = (amount*25)/100;
         topaid = amount-discount;
      }
      cout<<"\nYou've to Pay Only "<<topaid;
   }
   cout<<endl;
   return 0;
}

Here is its sample run with user input 12699 as total shopping amount:

calculate discount purchase amount to paid c++

C++ Online Test


« C++ Tutorial C++ Example »



© Copyright 2021. All Rights Reserved.

CodesCracker