How to calculate buy two get one free Blu-rays?

I am attempting to determine how to set up and equation to calculate a buy two get one free deal.

Blu-ray discs are selling at 9.99 and after you buy two there is a free blu-ray thrown in.

the outcome should look like this....

"How many Blu-ray discs are you buying? 7
Total price: $49.95
Press any key to continue . . ."

and

"How many Blu-ray discs are you buying? 8
Total price: $59.94
Press any key to continue . . ."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	// Declare variables
	int numCds = 0;
    	double totPrice = 0.0;
		
	// Request input
	cout << "How many Blu-ray discs are you buying? ";
	cin >> numCds;
		
	// Calculate and display total		
    	totPrice = (numCds * 9.99)-(9.99*2);
     	cout << "Total Price: " << totPrice << endl;   
     
     	system("pause"); 
     	return 0;
}


I have figured out how to do it with the examples in the problem however I don't think its right. Thanks!
Last edited on
You can either take advantage of integer division with some clever formulas, or you can use an if/else on whether the amount is odd or even.

By the way, the topic title almost got me to mark your post as spam (we've been getting a lot lately).
Oh, my apologies! I didn't mean for it to sound like that.

It is requested that we use integer division for this problem. I do not understand as to how it would work though. I am not good with math at all so in my way of thinking this division doesn't really make sense. Would the % operator help here at all?
Buy-two-get-one means that every 3 CDs costs 2*9.99 = 19.98. So divide the number of CDs by 3 and multiply by 19.98. Any CDs left over cost 9.99 each.
Topic archived. No new replies allowed.