-4

A shop sells one chocolate @ Rs.1 each, and In exchange of 3 wrappers gives one chocolate again? I want to know the C program logic for this? Have tried it, but getting wrong result.

Output should look like:

Enter the amount given to shopkeeper in Rs. 50

-> Total number of chocolates: 74
-> Wrappers remaining: 2
cpp-coder
  • 185
  • 1
  • 2
  • 12
  • 1
    post code otherwise should we mindread? – jgr Nov 26 '13 at 11:54
  • Please elaborate on the example, I have no idea how you came up with these numbers, the problem is unclear. Is it just summation? – amit Nov 26 '13 at 11:54
  • Please don't ask for solutions to your homework. Post your code and ask for help if you face issues with the code. – Pankaj Nov 26 '13 at 12:22
  • I have my code in place, its not giving me the correct output, hence did not write it here. @Peter thanks for correcting. – cpp-coder Nov 26 '13 at 13:20
  • Welcome to Stackoverflow! Please read the [About](http://stackoverflow.com/about) page soon -- having been a member for 11 months, indications are you haven't done that yet. For now, read [How do I ask a good question?](http://meta.stackoverflow.com/help/how-to-ask). – Jongware Nov 26 '13 at 14:28

1 Answers1

2

your example is wrong:

50@ = 50 chocolate wrapper
-> 16 choc (and 2 wrapper left)
-> 5 choc (+1 wrapper left)
-> 1 chock (+2 wrapper left)
-> 5 wrapper -> 1 chock + 2left
-> 3 wrapper -> 1 chock

it should be 74

50@ -> 50c + 0w -> 0c + 50w -> 16c + 2w -> 0c + 18w -> 6c + 0w -> 0c + 6w -> 2c + 0w -> 0c + 2w.

pseudocode:

chock = money;
geschock = 0;
wrapper = 0;

while chock > 3 {
  geschock += chock;
  wrapper = modulo((chock+wrapper), 3);
  chock = (chock+wrapper) / 3;
}
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
  • int main() { int price=0, tot_choc=0, wrappers=0, choc_wrap=0; printf("Enter price in Rs."); scanf("%d",&price); tot_choc = price; wrappers = price; while (wrappers>=3) { choc_wrap=wrappers/3; tot_choc+=choc_wrap; wrappers=choc_wrap + (wrappers%3); } printf("\nWrappers left: %d",wrappers); printf("\nTotal no. of choclates: %d",tot_choc); getch(); return 1; } – cpp-coder Nov 26 '13 at 18:04