In: Computer Science
Using Java!
Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
import java.util.Scanner; public class Denominations2 { public static void main(String args[]){ double amt; int amount; int q = 0, d = 0, n = 0, c = 0; Scanner scanner = new Scanner(System.in); System.out.print("Enter a dollar amount: "); amt = scanner.nextDouble(); amount = (int)(amt*100); if (amount >= 25) { q = amount/25; amount = amount % 25; } if(q == 1){ System.out.println("1 quarter"); } else{ System.out.println(q+" quarters"); } if (amount >= 10) { d = amount/10; amount = amount % 10; } if(d == 1){ System.out.println("1 dime"); } else{ System.out.println(d+" dimes"); } if (amount >= 5) { n = amount/5; amount = amount % 5; } if(n == 1){ System.out.println("1 nickel"); } else{ System.out.println(n+" nickels"); } if(amount == 1){ System.out.println("1 cent"); } else{ System.out.println(amount+" cents"); } } }