4 Replies - 12932 Views - Last Post: 14 June 2010 - 05:36 PM Rate Topic: -----

#1 Guest_Buttons225*


Reputation:

Interest rate increment problem

Posted 14 June 2010 - 03:45 PM

I am having a bit of trouble with my homework and my professor is not responding to my emails. I have written the code and it runs, but does not give me the output that I desire.

Here is the question from the text: "Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8."

I cannot seem to figure out how to start at 5% and stop at 8% with a 1/8 increment increase.

Here is what I have so far:

 
import java.util.Scanner;

public class Test {
	
  public static void main(String[] args) {
	  
	  Scanner input = new Scanner(System.in);  

	  System.out.print("Enter loan amount: ");
	  double loanAmount = input.nextDouble();

	  System.out.print("Enter number of years: ");
	  int numOfYears = input.nextInt();

	  double annualInterestRate = 5;

	  double monthlyInterestRate = annualInterestRate/1200;

	  double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));

	  double totalPayment = monthlyPayment * 12 *numOfYears;
	 

	  System.out.println("Loan Amount: " + loanAmount);
	  System.out.println("Number of Years: " + numOfYears);
	  
	  System.out.println();
	 

	  System.out.println("Interest Rate \t Monthly Payment \t Total Payment");
	  
	  double i = 5.0;
	  while (i <= 8.0){
		  i = i + 0.125;
		  i++;

      monthlyPayment = (int)(monthlyPayment * 100) / 100.0 ;
      totalPayment = (int)(totalPayment * 100) / 100.0;
      System.out.println(i +  "\t\t" + monthlyPayment + "\t\t" + totalPayment );
	  }
    }
  }





The output of this code is this:

Enter loan amount: 10000
Enter number of years: 5
Loan Amount: 10000.0
Number of Years: 5

Interest Rate Monthly Payment Total Payment
6.125 188.71 11322.74
7.25 188.71 11322.74
8.375 188.71 11322.74

As you can see the interest rates are not correct nor are the monthly payments or total payments. Any help would be very much appreciated as I have been staring at this for 4+ hours :sad2:

Is This A Good Question/Topic? 0

Replies To: Interest rate increment problem

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Interest rate increment problem

Posted 14 June 2010 - 04:43 PM

From what i can tell of your assignment, your loop(s) should be more like:

        double i = 5.0;
	int lastMonth = numYears * 12;
	int month = 1;

	while(month <= lastMonth) {
	    while (i <= 8.0) {
		i = i + 0.125;
		// Calc here
		i++;
	    }
	    month++
	}


Was This Post Helpful? 0
  • +
  • -

#3 Guest_Buttons225*


Reputation:

Re: Interest rate increment problem

Posted 14 June 2010 - 04:48 PM

That produces a result of:

Enter loan amount: 10000
Enter number of years: 5
Loan Amount: 10000.0
Number of Years: 5

Interest Rate Monthly Payment Total Payment
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
8.375 188.71 11322.74
.
.
.

I also cannot seem to get the Monthly Payments to increase with the change of interest rates (as in my previous code)
Was This Post Helpful? 0

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Interest rate increment problem

Posted 14 June 2010 - 05:23 PM

Sorry - getting tired here. Should be more like

	    while (i <= 8.0) {
		while(month <= lastMonth) {
		    // Calc here
		    System.out.printf("Interest rate=%.3f, month number = %d\n", i, month);
		    month++;
		}
		i += 0.125;
		month = 1;
	    }


This post has been edited by g00se: 14 June 2010 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Buttons225*


Reputation:

Re: Interest rate increment problem

Posted 14 June 2010 - 05:36 PM

I figured it out, Thank you very much for your help! Go get some rest :wink:
Was This Post Helpful? 0

Page 1 of 1