Results 1 to 1 of 1
Thread: Nested loop problem
- 04-17-2011, 03:38 AM #1
Member
- Join Date
- Sep 2010
- Location
- Southwest Missouri
- Posts
- 97
- Rep Power
- 0
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:
I have no idea where I am supposed to use this. Here is what I have:Java 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 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.Java 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"); } } }
Similar Threads
-
Nested Loop
By sehudson in forum New To JavaReplies: 2Last Post: 03-11-2011, 03:39 AM -
Issue with nested for loop
By sunshine64 in forum New To JavaReplies: 5Last Post: 02-03-2011, 02:45 AM -
Nested For Loop algorithm
By but43r in forum Advanced JavaReplies: 5Last Post: 01-11-2011, 08:52 AM -
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01-25-2010, 02:49 AM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks