# Thread: Calculating compound interest using ONLY integers.

1. Member
Join Date
Oct 2009
Posts
2
Rep Power
0

## 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

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

2. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
5
Originally Posted by AprilFlowers
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
Java Code:
```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

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
Java Code:
```System.out.printf( "%s%20s\n", "Year", "Amount on deposit" );

System.out.printf( "%4d%,20.2f\n", year, amount );```
that format seems to be similar to c

in java you dont declare"%d" etc , instead
Java Code:
```//display headers
System.out.printf( "Year\n"+ "Amount on deposit\n" );

// display the year and the amount
System.out.printf( "Year:"+ year+"\n"+"\nAmount"+ amount );```
is enough

3. Originally Posted by [RaIdEn]
in java you dont declare"%d" etc , instead
Sure we do declare these markup texts.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•