Hi, I'm brand new to the forum and am taking an intro to Java class and need tome help. I have never programmed before so go easy on me, lol.

The problem is to get the exact change from a dollar amount that a user inputs.
$4.23 is 3 cents,
0 nickels, 2 dimes,
0 quarters, 4 dollars,
0 fives, 0 tens,
0 twenties, 0 fifties,
and 0 hundreds.

What I am most confused about is the math behind it all. I converted the dollar amount into cents but them I'm not exaclty sure how to get all the dollar denominations. I know its something with dividing and carrying remainders but thats about it.
Here is what I have so far.

import java.util.Scanner;

public class Assg1

	public static void main (String[]arg) 
		Scanner kbd = new Scanner(System.in);
		double amntMoney;

		System.out.print("Enter amount of money.\n "
				+ "Include cents.\n"
				+ "$ ");
		amntMoney = kbd.nextDouble();

		double cents = amntMoney*100;


Thanks for any help.

Before coding you should work out the algorithm you want to code.
Given an amount, you find the largest coin the fits, compute out how many, subtract those from the total and do it again until nothing is left.

You should work from the highest amount of money down.

If the highest form of change is 100s, then first check how many 100s are in the number. Then however many there are, subtract it from the number.

i.e. if you have $532 you will end with 5 hundreds, and the number will have the value of $32. If you have $99 you will end with 0 hundreds, and the number will have the same value of $99 because nothing was subtracted.

Then after you have subtracted out the hundreds move onto your next highest form of change and do the same thing. Continue this pattern and whatever you are left with at the end would be your lowest form of change.

Good luck!

This article has been dead for over six months. Start a new discussion instead.