-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertCents.java
48 lines (40 loc) · 1.87 KB
/
ConvertCents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package codingProblems;
import java.util.Scanner;
public class ConvertCents {
final static int DOLLARS = 100; // number of cents in dollar
final static int QUARTERS = 25; // number of cents in quarter
final static int DIMES = 10; // number of cents in dime
final static int NICKELS = 5; // number of cents in nickel
public static void main (String[] args) {
int cents; // total amount of cents
int numDollars, numQuarters, // number of dollars, quarters
numDimes, numNickels; // number of dimes, nickels
int centsLeft; // cents left after coins
System.out.println("Cents to Coins Program");
System.out.println("----------------------");
// prompt the user to enter a total number of cents
Scanner in = new Scanner(System.in);
System.out.println("Enter total number of cents (positive integer): ");
cents = in.nextInt();
System.out.println();
// compute total amount of dollars, quarter, dimes, nickels, and pennies
numDollars = cents/DOLLARS;
centsLeft = cents % DOLLARS;
numQuarters = centsLeft/QUARTERS;
centsLeft = centsLeft % QUARTERS;
numDimes = centsLeft/DIMES;
centsLeft = centsLeft % DIMES;
numNickels = centsLeft/NICKELS;
centsLeft = centsLeft % NICKELS;
// display resulting number of coins
System.out.print("For your total cents of " + cents);
System.out.println(" you have:");
System.out.println("#dollars = " + numDollars);
System.out.println("#quarters = " + numQuarters);
System.out.println("#dimes = " + numDimes);
System.out.println("#nickels = " + numNickels);
System.out.println("#pennies = " + centsLeft);
System.out.println();
in.close();
}
}