Question:

What's wrong with this code? It's C and when I run it's just giving me back a ##Question_Title##.00.

by  |  earlier

0 LIKES UnLike

float monthlyPayment = 1 - pow((1 monthlyInterestRate), -numberPayments);

monthlyPayment = monthlyInterestRate / monthlyPayment;

monthlyPayment = loanAmount * monthlyPayment;

 Tags:

   Report

2 ANSWERS


  1. You are doing arithmetic division , cast everything to float or double.


  2. Post all your code if you can. This snap shot you posted doesn't have enough info.

    --Thanks for the code.  

    I see two more mistakes.  unsigned what?

    unsigned int loanAmount = 150000;

    unsigned int numberPayments = loanLength * MONTHS_IN_YEAR;

    A helpful hint would be to put cout statements in your code to see the values as they change.  You would spot the mistake by yourself.  Your monthlyPayment forumula is wrong.  I added the link where you can correct it.  I outputted the values as my debugger stepped through your code.

    /*

    * File:   YahooQuestion.cpp

    * Author: Chris

    *

    * Created on August 13, 2008, 8:37 PM

    */

    #include <stdlib.h>

    //mortgage.cpp - visual c++

    #include <iostream>

    Thanks for the code.

    #include <cmath>

    int main()

    {

    unsigned loanAmount = 150000;

    float annualInterestRate = 6.0;

    unsigned short loanLength = 30;

    const unsigned short MONTHS_IN_YEAR = 12;

    float monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR;

    //monthlyInterestRate is .5

    monthlyInterestRate = monthlyInterestRate/100;

    //monthlyInterestRate is 0.00499999989

    unsigned numberPayments = loanLength * MONTHS_IN_YEAR;

    //numberPayment is 360

    float monthlyPayment = 1 - pow((1 + monthlyInterestRate), -numberPayments);

    //negative Infinity...This jumps out as wrong.  I added the //correct forumla below.

    monthlyPayment = monthlyInterestRate/monthlyPayment;

    //monthlyPayment is -0 because of the earlier neg infinity.

    monthlyPayment = loanAmount * monthlyPayment;

    //monthlyPayment is -0  because of neg infinity.

    std::cout << "Assuming a loan in the amount of $" << loanAmount << ", at " << annualInterestRate << "% interest, over " << loanLength << " years, the monthly payment would be $";

    std::cout << monthlyPayment << ".\n\n";

    std::cout.setf(std::ios_base::fixed);

    std::cout.setf(std::ios_base::showpoin...

    std::cout.precision(2);

    http://www.hughchou.org/calc/formula.htm...

    Okay now for the big monthly payment (M) formula, it is:

             M  =  P  x (J/ (1  - ( 1 + J ) ^ -N)

                    

                          

    First you must define some variables to make it easier to set up:

    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.

    The following assumes a typical conventional loan where the interest is compounded monthly. First I will define two more variables to make the calculations easier:

    J = monthly interest in decimal form = I / (12 x 100)

    N = number of months over which loan is amortized = L x 12

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.