← Back to Community
##### C++ 18.3 18.2 Practice „Who Doesn't Love A Discount?“ (Sololearn)
Profile icon
FallingNever

C++ 18.3 18.2 Practice „Who Doesn't Love A Discount?“

do-while

A supermarket has launched a campaign offering a 15% discount on 3 items.
Write a program that takes the total price for every purchase as input and outputs the relevant discount.

Sample Input
15000
4000
6700

Sample Output
2250
600
1005

Explanation
2250 represents a 15% discount for a 15000 purchase; 600 for 4000 and 1005 for 6700 accordingly.
​ Use cin inside the loop to the get an input for every iteration.

The given code was:
#include
using namespace std;

int main(){
int purchaseAmount = 0;
int totalPrice;

return 0;
}

I added following lines to the code:

int i = 0;
do {
cin >> totalPrice;
if (totalPrice%2==0){
purchaseAmount = totalPrice * 0.15;
}
else{
purchaseAmount = (totalPrice * 15.0)/100.0;
}
cout << purchaseAmount << endl;
i++;
} while (i < 3);
return 0;

So in the end this was written there:

#include
using namespace std;

int main(){
int purchaseAmount = 0;
int totalPrice;

int i = 0;
do {
cin >> totalPrice;
if (totalPrice%2==0){
purchaseAmount = totalPrice * 0.15;
}
else{
purchaseAmount = (totalPrice * 15.0)/100.0;
}
cout << purchaseAmount << endl;
i++;
} while (i < 3);
return 0;
}

The first test case was:

Test Case 1
Input
1500
1680
6930
225
252
1039
Expected Output
225
252
1039.5

I understood what I did wrong, so I added the if/else statement to the code. The if statement says if (totalPrice%2==0), so when the number is even, do execute this code: purchaseAmount = totalPrice * 0.15;
This outputs an integer so I also write an else statement, which will be executed if the number is not even. If the number is not even, this code will be executed: purchaseAmount = (totalPrice * 15.0)/100.0;
I thought that this code will output an float (or double), but it did not. And actually, I haven’t learned about floating point numbers in this C++ course.
For example test case 2 was everything right:
Test Case 2
Input
9400
16800
14980