# using integers

• September 20th, 2012, 03:45 AM
Kareem Mesbah
using integers
hey guys what's up?

I was trying to solve a problem that I met in my book Java how to program it's about compound interests.

Code :

```  public class Interests {   public static void main (String[] args) {   double p = 1000; double rate = 0.05; double amount; for (int year = 1; year <= 10; year++){   amount = p * (Math.pow((1+rate),years)); System.out.printf("the amount for year %d is: %,20.2f", year, amount); }//end for   }//end main   }//end class```

this was the original code. THE QUESTION IS: Modify the application 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 dollars and cents portions by using the division and remainder
operations, respectively. Insert a period between the dollars and the cents portions.]

THE PROBLEM IS: it's ok to treat all monetary amounts as integral numbers of pennies then break the result into dollars and cents, but how can I convert the rate to an integer without losing data ?
• September 20th, 2012, 08:19 AM
Norm
Re: using integers
Quote:

but how can I convert the rate to an integer without losing data ?
Can you post the program's output and show what the output should be?
5% = 5 / 100
• September 20th, 2012, 10:46 AM
Kareem Mesbah
Re: using integers
actually 5/100 cannot be an integer

that output should be the amount in pennies then I've to convert it using the reminder and the division operators to dollars and cents.
for example, if the p = 1000\$, the year = 2, the rate = 0.05 I should get 1102.5\$
what should I convert to: the p = 100000 pennies, the year = 2, I don't know what should the rate be but after calculating I should get an integer which should be 110250 ,then I've separate the dollars and the cents using the reminder and the division operator (I already know how to do this).
• September 20th, 2012, 10:54 AM
Norm
Re: using integers
Are you getting the desired answer now?
I don't see where you posted the output from the program and explain what is wrong with it and what you want the output to be.