1. Member
Join Date
Dec 1969
Posts
49

## buy 2 get 1 free

Hi,<BR>I&#039;m trying to write code for a book promotion we&#039;re doing, and I&#039;m having a hard time figuring out where to start. Basically, all of these books are \$20 and if you buy 2 you get the third free. So if you buy 6, you get books 3 and 6 free. Can anyone help?<BR><BR>Thanks :)

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## What part are you struggling with?

The mathematical formula?<BR><BR>If so, check out what MOD can do for ya....

3. Member
Join Date
Dec 1969
Posts
49

## RE: What part are you struggling with?

I was actually tinkering w/ that...just can&#039;t figure out HOW to use it. I know if it&#039;s going to be a multiple of 3 then I just take away every 3rd book, I just don&#039;t know how to get that down in code.<BR>Thanks

4. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: What part are you struggling with?

numberOfBooks = 5 &#039; or whatever<BR>priceOfBooks = 20<BR><BR>totalPrice = (numberOfBooks * priceOfBooks) - ((numberOfBooks 3) * priceOfBooks)<BR>&#039; is integer division<BR><BR>Probably better ways of doing it -- I&#039;m kinda tired now and don&#039;t feel like thinking too much today :)

5. Member
Join Date
Dec 1969
Posts
49

## RE: What part are you struggling with?

Got it! Thanks!!

6. Senior Member
Join Date
Dec 1969
Posts
3,195

## Should that be something like

numberOfBooks = 5 &#039; or whatever <BR>priceOfBooks = 20 <BR>numBooksToCharge= numberOfBooks - (fix(numberOfBooks/3))<BR><BR>totalPrice = numBooksToCharge * priceOfBooks <BR><BR>Obviously this is if the price of books is ALWAYS \$20. If he wants to give them the lowest priced book free or some other formula then it changes.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•