53.3k views
3 votes
Write a method named coinConverter that accepts a single integer parameter representing some number of pennies and prints the corresponding number of dollars, quarters, dimes, nickels, and any leftover pennies. The method, named coinConverter(), should be designed to accept an integer parameter. For instance, calling coinConverter(1234) would produce the following output:

Dollar bills: 12
Quarters: 1
Dimes: 0
Nickels: 1
Pennies: 4

Test your method by invoking it in the program's main method. Ensure to remove or comment out the main method before checking your code for a score.

User Odetta
by
8.0k points

1 Answer

5 votes

Final answer:

To convert the given number of pennies into dollars, quarters, dimes, nickels, and pennies, you can use the division and mod operator. The code provided can solve the problem.

Step-by-step explanation:

To solve this problem, we can use division and mod operator. Here's the code:

public static void coinConverter(int pennies) {
int dollarBills = pennies / 100;
pennies = pennies % 100;
int quarters = pennies / 25;
pennies = pennies % 25;
int dimes = pennies / 10;
pennies = pennies % 10;
int nickels = pennies / 5;
pennies = pennies % 5;

System.out.println("Dollar bills: " + dollarBills);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
}

If we call coinConverter(1234), it will print:

Dollar bills: 12
Quarters: 1
Dimes: 0
Nickels: 1
Pennies: 4

User Josiekre
by
7.1k points