Write a java program named: CoinConverter.java
(Note: We do not need loops for HW 4! We haven't yet covered them when it was assigned.)
This program will prompt the user for a number of cents, reading in the value from the keyboard. The number entered should be an integer.
Using the value entered, calculate and print the number of quarters, dimes, nickels and/or pennies needed to achieve the value entered. If no coins of a denomination are needed, print 0 that value (e.g., if only quarters and pennies are needed, print "0 dimes" and "0 nickels"). Make sure you print only "quarter", "dime", "nickel" or "penny" when only 1 coin is required.
Enter a number of cents, and I'll tell which coins are needed to achieve that value:
67
2 quarters
1 dime
1 nickel
2 pennies
Another example:
import java.util.Scanner;
public class Main {
final static int quarters = 25;
final static int dimes = 10;
final static int nickels = 5;
public static void main (String[] args) {
int coins;
int n_Quarters,
n_Dimes, n_Nickels;
int coinsLeft;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter total number of coins (positive integer): ");
coins = keyboard.nextInt();
System.out.println();
n_Quarters = coins/quarters;
coinsLeft = coins % quarters;
n_Dimes = coinsLeft/dimes;
coinsLeft = coinsLeft % dimes;
n_Nickels = coinsLeft/nickels;
coinsLeft = coinsLeft % nickels;
System.out.println(n_Quarters+" quarters");
System.out.println(n_Dimes+" dimes");
System.out.println(n_Nickels+" nickels");
System.out.println(coinsLeft+" pennies");
}
}
Comments
Leave a comment