Java Reference
In-Depth Information
10
"Enter an amount, for example, 11.56: " );
double amount = input.nextDouble();
enter input
11
12
13
int remainingAmount = ( int )(amount * 100 );
14
15
// Find the number of one dollars
dollars
16
17 remainingAmount = remainingAmount % 100 ;
18
19
int numberOfOneDollars = remainingAmount / 100 ;
// Find the number of quarters in the remaining amount
quarters
20
21 remainingAmount = remainingAmount % 25 ;
22
23
int numberOfQuarters = remainingAmount / 25 ;
// Find the number of dimes in the remaining amount
dimes
24
25 remainingAmount = remainingAmount % 10 ;
26
27
int numberOfDimes = remainingAmount / 10 ;
// Find the number of nickels in the remaining amount
nickels
28
29 remainingAmount = remainingAmount % 5 ;
30
31
int numberOfNickels = remainingAmount / 5 ;
// 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 \n" +
36
output
"\t" + numberOfOneDollars + " dollars\n" +
37
"\t" + numberOfQuarters + " quarters\n" +
38
"\t" + numberOfDimes + " dimes\n" +
39
"\t" + numberOfNickels + " nickels\n" +
40
"\t" + numberOfPennies + " pennies" );
41 }
42 }
Enter an amount, for example, 11.56:
Your amount 11.56 consists of
11 dollars
2 quarters
0 dimes
1 nickels
1 pennies
11.56
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
 
Search WWH ::




Custom Search