Calculating compound interest using ONLY integers.
I have an assignment to alter a previous assignment (compounding interest over 10 years at 5%) by only using integers.
So i know that 5% is equal to dividing by 100 and multiplying by 5. And I know I have to convert it back from pennies to dollars. I tried using the Math.pow but it just gave me a lot more errors. I am just lost, I'm not very good at this. If someone could give me a hint and let me know where to go from here, I would appreciate it. Right now it just prints "Year Amount on Deposit" and then says:
1Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:3992)
at java.util.Formatter$FormatSpecifier.printFloat(For matter.java:2721)
at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2666)
at java.util.Formatter.format(Formatter.java:2432)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at InterestTwo.main(InterestTwo.java:22)
This is my code:
// Fig. 5.6: InterestTwo.java
// Compound interest calculations with for using only integers
public class InterestTwo
{
public static void main( String[] args )
{
int amount = 0; // amount on deposit at end of each year
int principal = 100000; // initial amount before interest
int rate = 5; // 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++ )
{
// calculate new amount for specified year
amount = principal / 100 * rate;
// display the year and the amount
System.out.printf( "%4d%,20.2f\n", year, amount );
} // end for
} // end main
} // end class InterestTwo.java
*sigh* thanks