Post: INEED help ASAP!!!
02-04-2014, 12:47 AM #1
HaZeYMan
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); i have this homework assignment for my java class lol i need help with it.

Write a java program to compute the coins necessary to return change to a customer after a purchase.

The change can be made up the following coins: quarters, dimes, nickels, and pennies.

For example:
Input:
Enter amount of change in cents: 69

Output:
Change:
quarters: 2
Dimes: 1
nickels: 1
pennies: 4

Note: you are NOT allowed to use IF - ELSE or LOOPs statements in this program.
(adsbygoogle = window.adsbygoogle || []).push({});
02-04-2014, 07:55 PM #2
Just4Hax
"I will speak ill of
The trick seems to be to use the mod calculation %. Create four int variables, use division and mod to work through it. Remember ints will drop off everything starting at the decimal.

Examples:
Int remainder = 69
Int quarters = 0;
quarters = remainder / 25;
remainder -= quarters *25;

The following user thanked Just4Hax for this useful post:

HaZeYMan
02-04-2014, 09:35 PM #3
Complete Speed
Do a barrel roll!
I don't work with java, I did this exact thing in python. don't know. but i think java has ternary operators don't know if they were legal or not in this project. this is how I did it in python

    

def main():

purchamt, payamt = inputdata()
change, dollars, quarters, dimes, nickels, pennies = calcchange(purchamt, payamt)
printresults (change, dollars, quarters, dimes, nickels, pennies)
return

def inputdata():
purchamt=float(raw_input("Enter the cost of the item purchased: "))
payamt=float(raw_input("Enter the amount paid for the item: "))
return purchamt, payamt



def calcchange(purchamt, payamt):
change = payamt - purchamt

cents = change*100+0.01

print cents
dollars = int(cents /100)
cents = cents %100
quarters = int(cents/25)
cents = cents %25
dimes = int(cents/10)
cents = cents %10
nickels = int(cents/5)
cents = cents %5
pennies = int(cents)

return change, dollars, quarters, dimes, nickels, pennies



def printresults(change, dollars, quarters, dimes, nickels, pennies):


print "Amount of change to give back: ", change
print 'dollars: $', dollars
print 'quarters: ', quarters
print 'dimes: ', dimes
print 'nickels: ', nickels
print 'pennies: ', pennies


return



main()

The following user thanked Complete Speed for this useful post:

HaZeYMan

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo