Java Reference
In-Depth Information
21 remainingAmount = remainingAmount % 25 ;
22
23 // Find the number of dimes in the remaining amount
24 int numberOfDimes = remainingAmount / 10 ;
25 remainingAmount = remainingAmount % 10 ;
26
27 // Find the number of nickels in the remaining amount
28 int numberOfNickels = remainingAmount / 5 ;
29 remainingAmount = remainingAmount % 5 ;
30
31
dimes
nickels
// Find the number of pennies in the remaining amount
pennies
32
int numberOfPennies = remainingAmount;
33
34 // Display results
35 System.out.println( "Your amount " + amount + " consists of" );
36 System.out.println( " " + numberOfOneDollars + " dollars" );
37 System.out.println( " " + numberOfQuarters + " quarters " );
38 System.out.println( " " + numberOfDimes + " dimes" );
39 System.out.println( " " + numberOfNickels + " nickels" );
40 System.out.println( " " + numberOfPennies + " pennies" );
41 }
42 }
output
Enter an amount, for example, 11.56: 11.56
Your amount 11.56 consists of
11 dollars
2 quarters
0 dimes
1 nickels
1 pennies
line#
11
13
16
17
20
21
24
25
28 29 32
variables
amount
11.56
remainingAmount
1156
56
6
6
1
numberOfOneDollars
11
numberOfQuarters
2
numberOfDimes
0
numberOfNickels
1
numberOfPennies
1
The variable amount stores the amount entered from the console (line 11). This variable
is not changed, because the amount has to be used at the end of the program to display the
results. The program introduces the variable remainingAmount (line 13) to store the chang-
ing remaining amount.
The variable amount is a double decimal representing dollars and cents. It is converted to
an int variable remainingAmount , which represents all the cents. For instance, if amount
 
Search WWH ::




Custom Search