I have written a program that takes in some amount of money, and prints the equivalent change (starting at hundred dollars, to fifty, to twenty, down the pennies). Here is the code:
System.out.print("Enter amount of money: ");
Scanner scan = new Scanner(System.in);
double value = scan.nextDouble();
int valueIntegral = (int) value;
int valueFractional = (int) Math.round(100 * value - 100 * valueIntegral);
// Integral values
int hundred = valueIntegral / 100;
int fifty = (valueIntegral % 100) / 50;
int twenty = ((valueIntegral % 100) % 50) / 20;
int ten = (((valueIntegral % 100) % 50) % 20) / 10;
int five = ((((valueIntegral % 100) % 50) % 20) % 10) / 5;
int one = (((((valueIntegral % 100) % 50) % 20) % 10) % 5) / 1;
// Fractional values
int quarter = valueFractional / 25;
int dime = (valueFractional % 25) / 10;
int nickel = ((valueFractional % 25) % 10) / 5;
int penny = (((valueFractional % 25) % 10) % 5) / 1;
System.out.println(hundred + " hundred dollar bills\n" +
fifty + " fifty dollar bills\n" +
twenty + " twenty dollar bills\n" +
ten + " ten dollar bills\n" +
five + " five dollar bills\n" +
one + " one dollar bills\n" +
quarter + " quarters\n" +
dime + " dimes\n" +
nickel + " nickels\n" +
penny + " pennies");
What I want to know is that without using loops or any iterative structures is this an acceptable way to accomplish this task? What would be a more elegant way? (those chains of modulo division are ugly)