Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Prog Proj #1; Chocolate

phillyluvr
phillyluvr 


Chocolate Suppose we can buy a chocolate bar from the vending machine for $1 each. Inside every chocolate bar is a coupon. We can redeem 7 coupons for one chocolate bar from the machine. We would like to know how many chocolate bars can be eaten, including those redeemed via coupon, if we have n dollars. For example, if we have 20 dollars then we can initially buy 20 chocolate bars. This gives us 20 coupons. We can redeem 14 coupons for 2 additional chocolate bars. These two additional chocolate bars have 2 more coupons, so we now have a total of 8 coupons when added to the six leftover from the original purchase. This gives us enough to redeem for one final chocolate bar. As a result we now have 23 chocolate bars and 2 leftover coupons. Write a program that inputs the number of dollars and outputs how many chocolate bars you can collect after spending all your money and redeeming as many coupons as possible. Also output the number of leftover coupons. The easiest way to solve this problem is to use a loop. Allow your program run continuously until the user wants to quit. CodeMate Hint: Use a while loop that redeems coupons for bars and calculates the new number of coupons. Continue the loop as long as you have enough coupons to redeem for a chocolate bar. Dollars Chocolate Bars Leftover Coupon 8 9 2 12 13 6 13 15 1 50 58 2 Attachments Tags Clarifications

Answers

Chocolates and coupons Solution

ravi_singh
ravi_singh 



Hi
Your Solution is provided in Java
Please run Chocolate.java file.

in While loop you need to write this
while(numCoup >=7 ){
numCoup -=7;
numChoco +=1;
numCoup+=1;
}

Complete Program is there in Chocolate.java File
Chocolate2.java file uses following while loop
while(numCoup >=7 ){
numChoco +=numCoup/7;
numCoup = numCoup%7 + numCoup/7;
}
which is more efficient than the previous method.
checkout that Chocolate2.java file too.
Attachments
Chocolate.java (1 K) Download Preview
int numCoup = N; int numChoco = N; while(numCoup >=7 ){ numCoup -=7; numChoco +=1;

chcoco.png (8 K) Download Preview

Chocolate2.java (1 K) Download Preview
: "); int N = in.nextInt(); int numCoup = N; int numChoco = N; while(numCoup >=7

Screenshots
Screenshot
Purchase Answer