This is what i have so far.. i've figured out how to do the mortgage payment but I am having trouble displaying the loan balance and interest paid for each payment over the term of the loan. can someone please help with the code and formulas?

Here's the code i have thus far.

/*Heather Davison

 */
package mortgagecalc;

/* A program written in Java that will calculate and display the monthly payment amount to a $200,000.00 loan over a 30 year term at a 5.75‰ interest rate. */

import java.lang.Math.*;

/* Program uses the Math class to perform mathematical functions. */

public class Main //Beginning
{
    public static void main(String[] args) //tells the program what to do
    {
        /* P = principal, the initial amount of the loan
           I = the annual interest rate (from 1 to 100 percent)
           L = length, the length (in years) of the loan, or at least the length over which the loan is amortized.
           J = monthly interest in decimal form = I / (12 x 100)
           N = number of months over which loan is amortized = L x 12
           M = the monthly payment

           M = P * ( J / (1 - (1 + J) ** -N))

           Above are the definitions and terms to create an equation to calculate the mortgage payment.
         http://www.hughchou.org/calc/formula.html
        */

        double dblmonthlypayment; // payment
        double dblprincipal=200000; // Amount of principal
        int intyears=30; // Number of years in loan
        int intnumberOfMonthsAmortize; // Number of Months
        double dblinterest=5.75; // Interest Rate
        double dblmonthlyInterest; // Monthly Interest Rate






        dblmonthlyInterest = dblinterest/(12*100); //J monthly interest
        intnumberOfMonthsAmortize = intyears*12;   //N number of months
        dblmonthlypayment = dblprincipal*(dblmonthlyInterest / (1 - Math.pow((1 + dblmonthlyInterest), -intnumberOfMonthsAmortize))); //     Forumla  M = P * ( J / (1 - (1 + J) ** -N))


              System.out.println();                            //spacing
              System.out.println("Principal of the loan = $200,000");// displays as is
              System.out.println("Loan length = 30 years"); // displays as is
              System.out.println("Loan Interest = 5.75%"); // displays as is
              System.out.println("Monthly payment = $ " +dblmonthlypayment); // displays as is and monthly payment result based on the calculation
              System.out.println();                            // spacing
    }


}

Edited 3 Years Ago by Dani: Formatting fixed

When posting code please surround it with code tags. Use icon above to right.

Can you post specific questions about where you are having trouble?
If you get compile or execution errors, copy and paste the full text here.

If the program's output is not what you want, show the output and add comments describing why it is wrong and show what you want it to be.

Edited 6 Years Ago by NormR1: n/a

I don't have any errors with the above code. I just need help displaying the loan balance and interest paid. It'll be an addition to the above code... I don't know what to start off with.

need help displaying the loan balance and interest paid.

Do you have a design on how you want to display it?

None in particular. Just want to get the answer for the balance and interest paid correct

I guess I don't understand your question. You have several print outs in your program such as the following. Why not use the same code as this:
System.out.println("Monthly payment = $ " +dblmonthlypayment)

I'm needing to add code that will display the balAnce and interest paid.. It's going to be addition to the above code

Ok this is what ive come up with.

/*Week 2 Assignment
        Individual Assignment
        Service Request SR-mf-003
        Heather Davison
        POS420
 */
package mortgagecalc;

/* A program written in Java that will calculate and display the monthly payment amount to a $200,000.00 loan over a 30 year term at a 5.75‰ interest rate. */

import java.lang.Math.*;

/* Program uses the Math class to perform mathematical functions. */
public class Main //Beginning
{
    public static void main(String[] args) //tells the program what to do
    {
        /* P = principal, the initial amount of the loan
           I = the annual interest rate (from 1 to 100 percent)
           L = length, the length (in years) of the loan, or at least the length over which the loan is amortized.
           J = monthly interest in decimal form = I / (12 x 100)
           N = number of months over which loan is amortized = L x 12
           M = the monthly payment

           M = P * ( J / (1 - (1 + J) ** -N))

           Above are the definitions and terms to create an equation to calculate the mortgage payment.
         http://www.hughchou.org/calc/formula.html
        */

        double dblmonthlypayment; // payment
        double dblprincipal=200000; // Amount of principal
        int intyears=30; // Number of years in loan
        int intnumberOfMonthsAmortize; // Number of Months
        double dblinterest=5.75; // Interest Rate
        double dblmonthlyInterest; // Monthly Interest Rate






        dblmonthlyInterest = dblinterest/(12*100); //J monthly interest
        intnumberOfMonthsAmortize = intyears*12;   //N number of months
        dblmonthlypayment = dblprincipal*(dblmonthlyInterest / (1 - Math.pow((1 + dblmonthlyInterest), -intnumberOfMonthsAmortize))); //     Forumla  M = P * ( J / (1 - (1 + J) ** -N))


            System.out.println();                            //spacing
            System.out.println("Principal of the loan = $200,000");// displays as is
            System.out.println("Loan length = 30 years"); // displays as is
            System.out.println("Loan Interest = 5.75%"); // displays as is
            System.out.println("Monthly payment = $ " +dblmonthlypayment); // displays as is and monthly payment result based on the calculation
            System.out.println();                            // spacing


 //Declares and builds three new variables

 double loanBalance = 0;

 double interestPaid = 0;

 int lineCount = 20;

 loanBalance = dblprincipal - dblmonthlypayment;  



 //Starts loop statement,and declares formula for loan balance and interest paid

 while (loanBalance > 0) {



 //Displays the loan balance and interest paid

 System.out.println("The loan balance is: $" + loanBalance);

 System.out.println("The interest paid on the loan is: $" + interestPaid);



 loanBalance = loanBalance - dblmonthlypayment;
 interestPaid = dblmonthlypayment * dblmonthlyInterest;

 //Pauses screen

 if (lineCount > 0) {

 lineCount--;

 try {

 Thread.sleep(1500);

 } catch (InterruptedException e) {

 }

 }

 }

 //Stops loop statement

 if (loanBalance <= 0) {

 System.out.println("The loan balance is: $0.00");

        }
    }
 }

But the interest paid is not right.. this is what i'm getting when i run it.
Principal of the loan = $200,000
Loan length = 30 years
Loan Interest = 5.75%
Monthly payment = $ 1167.1457128870982

The loan balance is: $198832.8542871129
The interest paid on the loan is: $0.0
The loan balance is: $197665.70857422578
The interest paid on the loan is: $5.592573207584012
The loan balance is: $196498.56286133867
The interest paid on the loan is: $5.592573207584012
The loan balance is: $195331.41714845155
The interest paid on the loan is: $5.592573207584012
The loan balance is: $194164.27143556444
The interest paid on the loan is: $5.592573207584012
The loan balance is: $192997.12572267733
The interest paid on the loan is: $5.592573207584012
The loan balance is: $191829.98000979022
The interest paid on the loan is: $5.592573207584012
The loan balance is: $190662.8342969031
The interest paid on the loan is: $5.592573207584012
The loan balance is: $189495.688584016
The interest paid on the loan is: $5.592573207584012
The loan balance is: $188328.54287112888
The interest paid on the loan is: $5.592573207584012
The loan balance is: $187161.39715824177
The interest paid on the loan is: $5.592573207584012
The loan balance is: $185994.25144535466
The interest paid on the loan is: $5.592573207584012
The loan balance is: $184827.10573246755
The interest paid on the loan is: $5.592573207584012
The loan balance is: $183659.96001958044
The interest paid on the loan is: $5.592573207584012
The loan balance is: $182492.81430669333
The interest paid on the loan is: $5.592573207584012
The loan balance is: $181325.66859380621
The interest paid on the loan is: $5.592573207584012
The loan balance is: $180158.5228809191
The interest paid on the loan is: $5.592573207584012
The loan balance is: $178991.377168032
The interest paid on the loan is: $5.592573207584012
The loan balance is: $177824.23145514488
The interest paid on the loan is: $5.592573207584012
The loan balance is: $176657.08574225777
The interest paid on the loan is: $5.592573207584012
The loan balance is: $175489.94002937066
The interest paid on the loan is: $5.592573207584012
The loan balance is: $174322.79431648355
The interest paid on the loan is: $5.592573207584012
The loan balance is: $173155.64860359643
The interest paid on the loan is: $5.592573207584012
The loan balance is: $171988.50289070932
The interest paid on the loan is: $5.592573207584012
The loan balance is: $170821.3571778222
The interest paid on the loan is: $5.592573207584012
The loan balance is: $169654.2114649351
The interest paid on the loan is: $5.592573207584012
The loan balance is: $168487.065752048
The interest paid on the loan is: $5.592573207584012
The loan balance is: $167319.92003916088
The interest paid on the loan is: $5.592573207584012
The loan balance is: $166152.77432627376
The interest paid on the loan is: $5.592573207584012
The loan balance is: $164985.62861338665
The interest paid on the loan is: $5.592573207584012
The loan balance is: $163818.48290049954
The interest paid on the loan is: $5.592573207584012
The loan balance is: $162651.33718761243
The interest paid on the loan is: $5.592573207584012
The loan balance is: $161484.19147472532
The interest paid on the loan is: $5.592573207584012
The loan balance is: $160317.0457618382
The interest paid on the loan is: $5.592573207584012
The loan balance is: $159149.9000489511
The interest paid on the loan is: $5.592573207584012
The loan balance is: $157982.75433606398
The interest paid on the loan is: $5.592573207584012
The loan balance is: $156815.60862317687
The interest paid on the loan is: $5.592573207584012
The loan balance is: $155648.46291028976
The interest paid on the loan is: $5.592573207584012
The loan balance is: $154481.31719740265
The interest paid on the loan is: $5.592573207584012
The loan balance is: $153314.17148451554
The interest paid on the loan is: $5.592573207584012
The loan balance is: $152147.02577162842
The interest paid on the loan is: $5.592573207584012
The loan balance is: $150979.8800587413
The interest paid on the loan is: $5.592573207584012
The loan balance is: $149812.7343458542
The interest paid on the loan is: $5.592573207584012
The loan balance is: $148645.5886329671
The interest paid on the loan is: $5.592573207584012
The loan balance is: $147478.44292007998
The interest paid on the loan is: $5.592573207584012
The loan balance is: $146311.29720719287
The interest paid on the loan is: $5.592573207584012
The loan balance is: $145144.15149430576
The interest paid on the loan is: $5.592573207584012
The loan balance is: $143977.00578141864
The interest paid on the loan is: $5.592573207584012
The loan balance is: $142809.86006853153
The interest paid on the loan is: $5.592573207584012
The loan balance is: $141642.71435564442
The interest paid on the loan is: $5.592573207584012
The loan balance is: $140475.5686427573
The interest paid on the loan is: $5.592573207584012
The loan balance is: $139308.4229298702
The interest paid on the loan is: $5.592573207584012
The loan balance is: $138141.2772169831
The interest paid on the loan is: $5.592573207584012
The loan balance is: $136974.13150409597
The interest paid on the loan is: $5.592573207584012
The loan balance is: $135806.98579120886
The interest paid on the loan is: $5.592573207584012
The loan balance is: $134639.84007832175
The interest paid on the loan is: $5.592573207584012
The loan balance is: $133472.69436543464
The interest paid on the loan is: $5.592573207584012
The loan balance is: $132305.54865254753
The interest paid on the loan is: $5.592573207584012
The loan balance is: $131138.40293966042
The interest paid on the loan is: $5.592573207584012
The loan balance is: $129971.25722677332
The interest paid on the loan is: $5.592573207584012
The loan balance is: $128804.11151388622
The interest paid on the loan is: $5.592573207584012
The loan balance is: $127636.96580099913
The interest paid on the loan is: $5.592573207584012
The loan balance is: $126469.82008811203
The interest paid on the loan is: $5.592573207584012
The loan balance is: $125302.67437522493
The interest paid on the loan is: $5.592573207584012
The loan balance is: $124135.52866233783
The interest paid on the loan is: $5.592573207584012
The loan balance is: $122968.38294945074
The interest paid on the loan is: $5.592573207584012
The loan balance is: $121801.23723656364
The interest paid on the loan is: $5.592573207584012
The loan balance is: $120634.09152367654
The interest paid on the loan is: $5.592573207584012
The loan balance is: $119466.94581078945
The interest paid on the loan is: $5.592573207584012
The loan balance is: $118299.80009790235
The interest paid on the loan is: $5.592573207584012
The loan balance is: $117132.65438501525
The interest paid on the loan is: $5.592573207584012
The loan balance is: $115965.50867212816
The interest paid on the loan is: $5.592573207584012
The loan balance is: $114798.36295924106
The interest paid on the loan is: $5.592573207584012
The loan balance is: $113631.21724635396
The interest paid on the loan is: $5.592573207584012
The loan balance is: $112464.07153346686
The interest paid on the loan is: $5.592573207584012
The loan balance is: $111296.92582057977
The interest paid on the loan is: $5.592573207584012
The loan balance is: $110129.78010769267
The interest paid on the loan is: $5.592573207584012
The loan balance is: $108962.63439480557
The interest paid on the loan is: $5.592573207584012
The loan balance is: $107795.48868191848
The interest paid on the loan is: $5.592573207584012
The loan balance is: $106628.34296903138
The interest paid on the loan is: $5.592573207584012
The loan balance is: $105461.19725614428
The interest paid on the loan is: $5.592573207584012
The loan balance is: $104294.05154325718
The interest paid on the loan is: $5.592573207584012
The loan balance is: $103126.90583037009
The interest paid on the loan is: $5.592573207584012
The loan balance is: $101959.76011748299
The interest paid on the loan is: $5.592573207584012
The loan balance is: $100792.6144045959
The interest paid on the loan is: $5.592573207584012
The loan balance is: $99625.4686917088
The interest paid on the loan is: $5.592573207584012
The loan balance is: $98458.3229788217
The interest paid on the loan is: $5.592573207584012
The loan balance is: $97291.1772659346
The interest paid on the loan is: $5.592573207584012
The loan balance is: $96124.0315530475
The interest paid on the loan is: $5.592573207584012
The loan balance is: $94956.88584016041
The interest paid on the loan is: $5.592573207584012
The loan balance is: $93789.74012727331
The interest paid on the loan is: $5.592573207584012
The loan balance is: $92622.59441438621
The interest paid on the loan is: $5.592573207584012
The loan balance is: $91455.44870149912
The interest paid on the loan is: $5.592573207584012
The loan balance is: $90288.30298861202
The interest paid on the loan is: $5.592573207584012
The loan balance is: $89121.15727572492
The interest paid on the loan is: $5.592573207584012
The loan balance is: $87954.01156283783
The interest paid on the loan is: $5.592573207584012
The loan balance is: $86786.86584995073
The interest paid on the loan is: $5.592573207584012
The loan balance is: $85619.72013706363
The interest paid on the loan is: $5.592573207584012
The loan balance is: $84452.57442417654
The interest paid on the loan is: $5.592573207584012
The loan balance is: $83285.42871128944
The interest paid on the loan is: $5.592573207584012
The loan balance is: $82118.28299840234
The interest paid on the loan is: $5.592573207584012
The loan balance is: $80951.13728551524
The interest paid on the loan is: $5.592573207584012
The loan balance is: $79783.99157262815
The interest paid on the loan is: $5.592573207584012
The loan balance is: $78616.84585974105
The interest paid on the loan is: $5.592573207584012
The loan balance is: $77449.70014685395
The interest paid on the loan is: $5.592573207584012
The loan balance is: $76282.55443396686
The interest paid on the loan is: $5.592573207584012
The loan balance is: $75115.40872107976
The interest paid on the loan is: $5.592573207584012
The loan balance is: $73948.26300819266
The interest paid on the loan is: $5.592573207584012
The loan balance is: $72781.11729530556
The interest paid on the loan is: $5.592573207584012
The loan balance is: $71613.97158241847
The interest paid on the loan is: $5.592573207584012
The loan balance is: $70446.82586953137
The interest paid on the loan is: $5.592573207584012
The loan balance is: $69279.68015664427
The interest paid on the loan is: $5.592573207584012
The loan balance is: $68112.53444375718
The interest paid on the loan is: $5.592573207584012
The loan balance is: $66945.38873087008
The interest paid on the loan is: $5.592573207584012
The loan balance is: $65778.24301798298
The interest paid on the loan is: $5.592573207584012
The loan balance is: $64611.097305095886
The interest paid on the loan is: $5.592573207584012
The loan balance is: $63443.95159220879
The interest paid on the loan is: $5.592573207584012
The loan balance is: $62276.80587932169
The interest paid on the loan is: $5.592573207584012
The loan balance is: $61109.660166434594
The interest paid on the loan is: $5.592573207584012
The loan balance is: $59942.5144535475
The interest paid on the loan is: $5.592573207584012
The loan balance is: $58775.3687406604
The interest paid on the loan is: $5.592573207584012
The loan balance is: $57608.2230277733
The interest paid on the loan is: $5.592573207584012
The loan balance is: $56441.077314886206
The interest paid on the loan is: $5.592573207584012
The loan balance is: $55273.93160199911
The interest paid on the loan is: $5.592573207584012
The loan balance is: $54106.78588911201
The interest paid on the loan is: $5.592573207584012
The loan balance is: $52939.640176224915
The interest paid on the loan is: $5.592573207584012
The loan balance is: $51772.49446333782
The interest paid on the loan is: $5.592573207584012
The loan balance is: $50605.34875045072
The interest paid on the loan is: $5.592573207584012
The loan balance is: $49438.203037563624
The interest paid on the loan is: $5.592573207584012
The loan balance is: $48271.05732467653
The interest paid on the loan is: $5.592573207584012
The loan balance is: $47103.91161178943
The interest paid on the loan is: $5.592573207584012
The loan balance is: $45936.76589890233
The interest paid on the loan is: $5.592573207584012
The loan balance is: $44769.620186015236
The interest paid on the loan is: $5.592573207584012
The loan balance is: $43602.47447312814
The interest paid on the loan is: $5.592573207584012
The loan balance is: $42435.32876024104
The interest paid on the loan is: $5.592573207584012
The loan balance is: $41268.183047353945
The interest paid on the loan is: $5.592573207584012
The loan balance is: $40101.03733446685
The interest paid on the loan is: $5.592573207584012
The loan balance is: $38933.89162157975
The interest paid on the loan is: $5.592573207584012
The loan balance is: $37766.745908692654
The interest paid on the loan is: $5.592573207584012
The loan balance is: $36599.60019580556
The interest paid on the loan is: $5.592573207584012
The loan balance is: $35432.45448291846
The interest paid on the loan is: $5.592573207584012
The loan balance is: $34265.30877003136
The interest paid on the loan is: $5.592573207584012
The loan balance is: $33098.163057144266
The interest paid on the loan is: $5.592573207584012
The loan balance is: $31931.01734425717
The interest paid on the loan is: $5.592573207584012
The loan balance is: $30763.87163137007
The interest paid on the loan is: $5.592573207584012
The loan balance is: $29596.725918482975
The interest paid on the loan is: $5.592573207584012
The loan balance is: $28429.580205595877
The interest paid on the loan is: $5.592573207584012
The loan balance is: $27262.43449270878
The interest paid on the loan is: $5.592573207584012
The loan balance is: $26095.288779821683
The interest paid on the loan is: $5.592573207584012
The loan balance is: $24928.143066934586
The interest paid on the loan is: $5.592573207584012
The loan balance is: $23760.99735404749
The interest paid on the loan is: $5.592573207584012
The loan balance is: $22593.851641160392
The interest paid on the loan is: $5.592573207584012
The loan balance is: $21426.705928273295
The interest paid on the loan is: $5.592573207584012
The loan balance is: $20259.5602153862
The interest paid on the loan is: $5.592573207584012
The loan balance is: $19092.4145024991
The interest paid on the loan is: $5.592573207584012
The loan balance is: $17925.268789612004
The interest paid on the loan is: $5.592573207584012
The loan balance is: $16758.123076724907
The interest paid on the loan is: $5.592573207584012
The loan balance is: $15590.977363837808
The interest paid on the loan is: $5.592573207584012
The loan balance is: $14423.83165095071
The interest paid on the loan is: $5.592573207584012
The loan balance is: $13256.68593806361
The interest paid on the loan is: $5.592573207584012
The loan balance is: $12089.540225176512
The interest paid on the loan is: $5.592573207584012
The loan balance is: $10922.394512289413
The interest paid on the loan is: $5.592573207584012
The loan balance is: $9755.248799402314
The interest paid on the loan is: $5.592573207584012
The loan balance is: $8588.103086515215
The interest paid on the loan is: $5.592573207584012
The loan balance is: $7420.957373628117
The interest paid on the loan is: $5.592573207584012
The loan balance is: $6253.811660741019
The interest paid on the loan is: $5.592573207584012
The loan balance is: $5086.665947853921
The interest paid on the loan is: $5.592573207584012
The loan balance is: $3919.5202349668234
The interest paid on the loan is: $5.592573207584012
The loan balance is: $2752.3745220797255
The interest paid on the loan is: $5.592573207584012
The loan balance is: $1585.2288091926273
The interest paid on the loan is: $5.592573207584012
The loan balance is: $418.0830963055291
The interest paid on the loan is: $5.592573207584012
The loan balance is: $0.00
BUILD SUCCESSFUL (total time: 30 seconds)

Edited 3 Years Ago by Dani: Formatting fixed

No.. i have something wrong with the interest paid formula or something.. it should not populate the same everytime. can you help me?

Please post your code in code tags. Use icon above to right.

something wrong with the interest paid formula or something

You'll have to identify exactly what is wrong to be able to fix it. Do the computations on a piece of paper and have your program print out the values and compare what you have on paper with what the computer prints out. You won't need to print out hundreds of lines, a few lines should show you where your computations are wrong.

This article has been dead for over six months. Start a new discussion instead.