In: Computer Science
For Java
3.25 LAB: Exact change
Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.
Ex: If the input is:
0
the output is:
No change
Ex: If the input is:
45
the output is:
1 Quarter 2 Dimes
270738.1591718
LAB ACTIVITY
3.25.1: LAB: Exact change
What did I do wrong here?
My Code:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num_Pennies;
int num_Nickels;
int num_Quarters;
int num_Dollars;
num_Pennies = scnr.nextInt();
if (num_Pennies == 0){
System.out.println("No Change");
}
else {
num_Dollars = num_Pennies / 100;
num_Quarters = ((num_Pennies - (num_Dollars * 100)) / 25);
num_Dimes = (num_Pennies - ((num_Dollars * 100) + (num_Quarters *
25))/ 10 );
num_Nickels = (num_Pennies - ((num_Dollars * 100) + (num_Quarters *
25) + (num_Dimes * 10)) / 5 );
num_Pennies = (num_Pennies - ((num_Dollars * 100) + (num_Quarters *
25) + (num_Dimes * 10) + (num_Nickels * 5)) / 5 );
if (num_Dollars == 1) {
System.out.println(num_Dollars + "Dollar");
}
else {
System.out.println(num_Dollars + "Dollars");
}
if (num_Quarters == 1) {
System.out.println(num_Quarters + "Quarter");
}
else {
System.out.println(num_Quarters + "Quarter");
}
if (num_Dimes == 1) {
System.out.println(num_Dimes + "Dime");
}
else {
System.out.println(num_Dimes + "Dimes");
}
if (num_Nickels == 1) {
System.out.println(num_Nickels + "Nickel");
}
else {
System.out.println(num_Nickel + "Nickels");
}
if (num_Pennies == 1) {
System.out.println(num_Pennies + "Penny");
}
else {
System.out.println(num_Pennies + "Pennies");
}
}
}
import java.util.Scanner; public class LabProgram { public static void ExactChange(int userTotal, int[] coinVals) { coinVals[0] = userTotal / 100; userTotal %= 100; coinVals[1] = userTotal / 25; userTotal %= 25; coinVals[2] = userTotal / 10; userTotal %= 10; coinVals[3] = userTotal / 5; userTotal %= 5; coinVals[4] = userTotal; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] coins = new int[5]; int value = in.nextInt(); if(value <= 0) { System.out.println("No change"); } else { ExactChange(value, coins); if (coins[0] == 1) System.out.println(coins[0] + " dollar"); else if (coins[0] >= 1) System.out.println(coins[0] + " dollars"); if (coins[1] == 1) System.out.println(coins[1] + " quarter"); else if (coins[1] > 1) System.out.println(coins[1] + " quarters"); if (coins[2] == 1) System.out.println(coins[2] + " dime"); if (coins[2] > 1) System.out.println(coins[2] + " dimes"); if (coins[3] == 1) System.out.println(coins[3] + " nickel"); if (coins[3] > 1) System.out.println(coins[3] + " nickels"); if (coins[4] == 1) System.out.println(coins[4] + " penny"); if (coins[4] > 1) System.out.println(coins[4] + " pennies"); } } }