gergomat
gergomat

Reputation: 57

Why doesn't program calculate the interest?

public class Interest {
    public static void main(String[] args) {
        int n;          // number of years
        double rate;    // annual rate

        for (int r = 5; r<=10; r++ ) {
            System.out.println("\nInterest rate is  " + r + "%");
            System.out.println("**********************");
            System.out.println("Year\tAmount on deposit");

            for (n=1; n<=10; n++) {
                int p = 1000; // original amount
                rate = r / 100;
                double a = p * (Math.pow(1 + rate, n)); // amount on deposit at the end of year
                System.out.printf("\n%d\t%.2f", n, a);
             }
         }
    }

}

It's showing the chart correctly in terms of years. But amount on deposit at the end of year (a) stays the same (1000).It looks like this;

Interest rate is  5%
**********************
Year    Amount on deposit

1         1000.00
2         1000.00
3         1000.00
4         1000.00
5         1000.00
6         1000.00
7         1000.00
8         1000.00
9         1000.00
10        1000.00

and it goes until rate reaches 10%.

Upvotes: 1

Views: 130

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

You're performing integer division.

Try : rate = r/100.0;

Upvotes: 13

MZ4Code
MZ4Code

Reputation: 76

Comment- When you divide two integers in java, the result is another integer (rounded down). Try using a double instead in your division.

Upvotes: 1

Related Questions