Java 5.18 Modified Compound Interest Program- Modify the application in Fig 5.6
ID: 3685138 • Letter: J
Question
Java 5.18 Modified Compound Interest Program- Modify the application in Fig 5.6 to use only integers to calculate the compound interest.[Hint: Treat all monetary amounts as integral numbers of pennies. Then break the result into its dollar and cents portions by using the division and remainder operations, respectively. Insert a period between the dollar and cents portion.] Ex. output $100.26
Fig 5.6
public class Modified_Compound_Interest_Program {
public static void main(String[] args) {
double amount; //amount on deposit at end of each year
double principal = 1000.0; //initial amount before interest
double rate = 0.05; //interest rate
//display headers
System.out.printf("%s%20s%n", "Year", "Amount on deposit");
//calculate amount on deposit for each of ten years
for (int year = 1; year <= 10; ++year)
{
//calcualte new amount for specified year
amount = principal * Math.pow(1.0 + rate, year);
//display the year and the amount
System.out.printf("%4d%,20.2f%n", year, amount);
}
}
} // end of class
Explanation / Answer
public class HelloWorld{
public static void main(String[] args) {
int dollar, cent; //amount on deposit at end of each year
double principal = 1000.0; //initial amount before interest
double rate = 0.05; //interest rate
double amount;
//display headers
System.out.printf("%s%20s%n", "Year", "Amount on deposit");
//calculate amount on deposit for each of ten years
for (int year = 1; year <= 10; ++year)
{
//calcualte new amount for specified year
amount = principal * Math.pow(1.0 + rate, year);
dollar = (int)(amount/100.0) ;
cent = (int)(amount%100.0);
//display the year and the amount
System.out.printf("%2d $%d.%d%n", year, dollar, cent);
}
}
} // end of class
output:
Year Amount on deposit
1 $10.50
2 $11.2
3 $11.57
4 $12.15
5 $12.76
6 $13.40
7 $14.7
8 $14.77
9 $15.51
10 $16.28
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.