# Nested loop problem

• 04-17-2011, 03:38 AM
jim01
Nested loop problem
I am completely lost on this particular program. I am supposed to write a program that finds the monthly payment for 30 years, for principals from
\$100,000 through \$200,000(with increments of \$20,000) and interest rates of
6% through 10% (with increments of 0.5%). The output of the file must be in a table that looks something like this:

Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
100000 600 632 665 699 733 768 804 840 877
120000 719 758 798 839 880 922 965 1009 1053
140000 … … … … … … … … …
160000 … … … … … … … … …
180000 … … … … … … … … …
200000 … … … … … … … … …

The monthly payment on mortgage loan of L dollars, at a rate of interest r is given by:

Monthly payment = [L(1 + r/12)exponent 12N ]/[(1 + r/12)exponent 12N - 1]

where N = number of year mortgage (30 years).

The professor gave us this hint:

Code:

```Hint: Use the following idea to compute (1 + r/12)exponent 12N double temp = 1+r/12; double temp1 = 1; for (int i = 1; i<=12*N;i++) { temp1 = temp1*temp; } The final formula now becomes: double mp = (L*(r/12)*temp1)/(temp1-1);```
I have no idea where I am supposed to use this. Here is what I have:

Code:

```public class ProgrammingProblemTwo {     public static void main(String[] args){             //declare variables                 double r = 0, N = 0, monthlyPayment = 0;                 double temp1 = 1;                 double sum;                 double temp = 1 + r / 12;                 System.out.println("Principal  6" + "%" + "  6.5" + "%" + "  7" + "%" +                         "  7.5" + "%" + "  8" + "%" + "  8.5" + "%" + "  9" + "%" + "  9.5" +                                   "%" + "  10" + "%\n");                 //create loop to increment principal in dollars, L                     for(int L = 100000; L <= 200000; L += 20000){                     System.out.println(L + "\n");                     //create loop to increment interest rate, r                             for(r = .06; r <= .101; r += .005){                                     //create (1 + r / 12) exponent 12N formula                                         for (int i = 1; i<=12*N;i++){                                                 temp1 = temp1*temp;                                                 monthlyPayment = (L*(r/12)*temp1)/(temp1-1);                                         }                                 }                 System.out.println(L + monthlyPayment + "\n");             }     } }```
I have tried putting "System.out.println(L + monthlyPayment + "\n");" within the brackets of each for loop with no success. I've been messing with this for several hours now and I'm at a loss. I'm pretty sure I have the loop for my principal (L) and the rate of interest (r) correct.