MakeChange

/** * Generates random number and determines the amount of change you will receive (in quarters, nickels, dimes, and pennies) if you give a 20 dollar bill * * @author Sameer * @version 1.0 */ public class MakeChange { public static void main (String args[]) { //Generate random number int max = 1900; int min = 100; int randomNum = (int)(Math.random() * ((max - min) + 1)) + min; //inclusive double myNum = randomNum/100.0; System.out.println("Random Number: " + myNum); //Calculate amount of change double change = 20.00 - myNum; System.out.println("Change: " + change); //Calculate amount of quarters for change int amtOfQuarters = (int)(change/.25); double amtLeft = change % .25; System.out.println("quarters: " + amtOfQuarters); //Calculate amount of dimes for change int amtOfDimes = (int)(amtLeft/.10); amtLeft = amtLeft % .10; System.out.println("dimes: " + amtOfDimes); //Calculate amount of nickels for change int amtOfNickels = (int)(amtLeft/.05); amtLeft = amtLeft % .05; System.out.println("nickels: " + amtOfNickels); //Calculate amount of pennies for change int amtOfPennies = (int)(amtLeft/.01); amtLeft = amtLeft % .01; System.out.println("pennies: " + amtOfPennies); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.