Java 5.18 Modified Compound Interest Program- Modify the application in Fig 5.6
ID: 3684877 • 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.]
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
step 1:
program : Compound interest in only integers
public class InterestInCents {
public static void main(String[] args) {
int amount; // amt on deposit at end of each year in cents
double amtDouble; // result of calculation; will be converted to int
int rate = 5; // interest rate of 5 cents
int principal = 100000; // one hundred thousand cents
// display headers
System.out.println(" Compound Interest in Cents");
System.out.printf("%s%20s ", "Year", "Amount on Deposit");
// calculate amt on deposit for each of 10 years
for (int year=1; year<=10; year++) {
amtDouble = principal * Math.pow(1+rate, year);
amount = (int) (Math.round(amtDouble));
System.out.printf("%4d%, 22d ", year, amount);
} // end for
// Now break everything down to dollars
System.out.println(" Compound Interest in Dollars ");
System.out.printf("%s%20s ", "Year", "Amount on Deposit");
for (int year=1; year<=10; year++) {
amtDouble = principal * Math.pow(1+rate, year);
amount = (int) (Math.round(amtDouble));
int dollars = amount / 100;
int cents = amount % 100;
// System.out.printf("%4d%, 20.2f ", year, dollars + "." + cents);
System.out.println(year + " " + dollars + "." + cents);
}
}
}
Output:
Compound Interest in Cents
Year Amount on Deposit
1 600,000
2 3,600,000
3 21,600,000
4 129,600,000
5 777,600,000
6 370,632,704
7 -2,071,171,072
8 457,875,456
9 -1,547,714,560
10 -696,352,768
Compound Interest in Dollars
Year Amount on Deposit
1 6000.0
2 36000.0
3 216000.0
4 1296000.0
5 7776000.0
6 3706327.4
7 -20711710.-72
8 4578754.56
9 -15477145.-60
10 -6963527.-68
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.