incorrect output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UncleAl
    New Member
    • Oct 2006
    • 3

    #1

    incorrect output

    Hello I am working a C++ program and the calculation is giving me the wrong total of 1317.2 when it should be 1167.1. Can you look at my code and algorithm to see what I am missing or doing wrong? I cant seem to figure this out. Thanks very much.

    Code:
    #include <iostream> 
    #include <math.h> 
    using namespace std; 
    int main() 
    { 
    int loan = 200000; 
    int y = 30; 
    int monthlyterm = y*12; 
    double i = .00575; //interest rate 
    double monthlypayment; 
    
    monthlypayment = (loan*i)/(1-pow(1+i,-monthlyterm)); 
    cout<<"monthly payment is $"<<monthlypayment; 
    return 0;
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I canconfirm that when compiled and run this code it prints $1317.2

    However checking by hand that is correct for the data and algorithm in the program.

    This means that either

    1. You have not implemented your algorithm correctly

    2. You have not got you constant values (e.g. the interest rate) correct

    3. The method you have used to calculate the expected result of the program is wrong.


    Since all you have posted is the program code and not any background information it is impossible to tell which one of these is the case in question.

    Comment

    • UncleAl
      New Member
      • Oct 2006
      • 3

      #3
      Originally posted by Banfa
      I canconfirm that when compiled and run this code it prints $1317.2

      However checking by hand that is correct for the data and algorithm in the program.

      This means that either

      1. You have not implemented your algorithm correctly

      2. You have not got you constant values (e.g. the interest rate) correct

      3. The method you have used to calculate the expected result of the program is wrong.



      Since all you have posted is the program code and not any background information it is impossible to tell which one of these is the case in question.
      Thanks I think 1 or 3 is my problem. If I use a mortgage calulator with 200,000 at 5.75 for 30 years it is 1,167 payment but my program gives 1317.2. How can I tell if 1 or 3 is my problem?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Right I see 2 problems which explain why you are not getting the same result.

        1. Your program looks like it may be attempting compound interest but my calculations suggest that your mortgage calculator is doing simple interest.

        In compound interest

        Year Interest Rate = pow(1+ monthly interest rate, 12) - 1

        In simple interest

        Year Interest Rate = 12 x monthly interest rate

        I also think that you have made the same mistake as me.

        Mortgage calculators normally work in years but your calculation is in months.

        I did this input to mortgage calculator

        principle: $200000
        term: 30
        rate: 5.75%

        but you are calculating in months with a monthly interest rate of 0.575%. Using simple interest this equates to 0.575x12 = 6.9%

        When you put

        principle: $200000
        term: 30
        rate: 6.9%

        into a mortgage calculator it spits out the monthly paymemt of $1317.20

        Which is what your program says.

        So the problem was in 3, calculating the expected result :D

        Comment

        • UncleAl
          New Member
          • Oct 2006
          • 3

          #5
          Originally posted by Banfa
          Right I see 2 problems which explain why you are not getting the same result.

          1. Your program looks like it may be attempting compound interest but my calculations suggest that your mortgage calculator is doing simple interest.

          In compound interest

          Year Interest Rate = pow(1+ monthly interest rate, 12) - 1

          In simple interest

          Year Interest Rate = 12 x monthly interest rate

          I also think that you have made the same mistake as me.

          Mortgage calculators normally work in years but your calculation is in months.

          I did this input to mortgage calculator

          principle: $200000
          term: 30
          rate: 5.75%

          but you are calculating in months with a monthly interest rate of 0.575%. Using simple interest this equates to 0.575x12 = 6.9%

          When you put

          principle: $200000
          term: 30
          rate: 6.9%

          into a mortgage calculator it spits out the monthly paymemt of $1317.20

          Which is what your program says.

          So the problem was in 3, calculating the expected result :D
          Thanks very very much. I have been trying to still get this to work with your guidance and can't seem to. At the risk of souding foolish, if my variables are as follows, can you help me get the algorithm right?

          int loan = 200000;
          int y = 30;
          int monthlyterm = y*12;
          double i = .00575; //interest rate
          double monthlypayment;

          monthlypayment = (loan*i)/(1-pow(1+i,-monthlyterm)); CAN'T FIGURE OUT !!!

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Erm, did you not understand my previous post? Assuming that the interest rate you are using is the monthly rate (and it seems in the correct order of magnitude to be the monthly rate) then you program was working correctly.

            Your calculation of the expect result was wrong.

            Comment

            Working...