Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)

 Happy new year all! 💥

I know it's pretty late but anyways I'm writing my first blog on this year. In this blog I'm going to share my assignment of making a minimarket discount program in C++. There will be two version of this program and here, I'm going to show you the first version. The purpose of this program is to check the total purchase amount against the specified thresholds to determine the applicable discount rate. It will also calculate the final price by subtracting the discount for the total purchase. As a reference to make this program, I was given a similar program. 

// program GradeConverter.cpp

#include <iostream>
#include <iomanip>

using namespace std;

int main (){

int score;
string grade;

cout << "Please input numerical score (1-100):";
cin >> score;

if(score <= 100){

grade = "A+";
}
else if (score >= 80 ){
grade = "A";
}
else if (score >= 70 ){
grade = "B";
}
else if (score >= 60){
grade = "C";
}
else grade = "D";

cout << "Numerical = " << score << endl;
cout  << "Grade = " << grade << endl;
}

The program above is grade converter program. The function of this program is to check the scores against the specified thresholds to determine the applicable grades. The requirements are if the score is more less than equal 100 the grade will be "A+", if the score is less than equal 80 the grade will be "A" and so on. Here we're going to input score 88 then we'll see what's the output. 
The output for numerical score = 88 is "A+" because 88 is less than equal 100 but more than 80. The algorithm for minimarket discount program is pretty the same but we need to add something to calculate the final price after we got the discount. Here's the code of minimarket discount program:

//program minimarket_discount_v1.cpp

#include <iostream>
#include <iomanip>

using namespace std;

int main (){

double totalPurchase, discount = 0.0, finalPrice;

cout << "Please enter the amount of purchase (in Rupiah): Rp";
cin >> totalPurchase;

if (totalPurchase >= 1000000){
    discount = 0.2;
}
else if (totalPurchase >= 500000){
    discount = 0.1;
}
else if (totalPurchase >= 200000){
    discount = 0.05;
}
else if (totalPurchase >= 100000){
    discount = 0.25;
}
    double discountAmount = totalPurchase * discount;
    finalPrice = totalPurchase - discountAmount;

cout << " --Shopping bill-- " << endl;
cout << "Total purchase: Rp" << totalPurchase << endl;
cout << "Discount: Rp" << discountAmount << endl;
cout << "Final price: Rp" << finalPrice << endl;
}

We use double here because we want to store decimal numbers with double precision for mathematical calculation because here, we're going to count the price after the discount. In line 27 discount amount is calculate by multiplying the total purchase and the discount. After that, in line 28 we calculate the final price by subtracting the total purchase and the discount amount. Here we are going to try input Rp250.000 for the total purchase and let's see the shopping bill including the total purchase, the discount amount, and the final price to be paid. 
The result of the discount amount is Rp12500 because it multiplies the total purchase (Rp250000) and the discount (5%). It got 5% discount because Rp250000 is more than 200000 and less than 500000. After that we got the final price is Rp237500 because it subtracts the total purchase and the discount amount which is 250000 - 12500 = 237500. 

So that's all I can share to you guys about making discount program in C++. I hope it can be useful for you and feel free to ask me on the comment section bellow. There will be some addition in the minimarket discount program version 2. Stay tuned and see you!

Komentar

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)