CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2014
    Posts
    22

    Math Doesn't Match

    I got another question of my latest lab done, but the math doesn't seem to work...

    Here is the code I have now.

    Code:
    #include <iostream> 
    #include <cmath> 
    using namespace std; 
    
    int main() 
    { 
    float A;
    float R;
    float L;
    float P;
    float N;
    
    cout << "Enter the loan amount: "; 
    cin >> L; 
    
    cout << "Enter the annual interest rate as a percent: "; 
    cin >> A; 
     
    cout << "Enter the number of payments: "; 
    cin >> N;
    
    R = (A / 12); 
    
    P = ((R * (pow (1 + R, N)))) / (((pow (1 + R, N) - 1)) * L); 
    
    cout << "Loan Amount: " << L << endl; 
    cout << "Monthly Interest Rate: " << R << endl; 
    cout << "Number of Payments: " << N << endl; 
    cout << "Monthly Payment: " << P << endl; 
    cout << "Amount Paid Back: " << P * N << endl;
    cout << "Interest Paid: " << P * N - L << endl;
    
    return 0; 
    }
    And here is what the output should be.

    Loan Amount: $10000.00
    Monthly Interest Rate: 1%
    Number of Payments: 36
    Monthly Payment: $ 332
    Amount Paid Back: $11957.15
    Interest Paid: $1957.15
    What am I not getting here?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Math Doesn't Match

    Set a breakpoint in the beginning of main(), start the debugger, debug your code step by step and see what happens with the variables after each step...
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Math Doesn't Match

    Quote Originally Posted by NearFruitcake View Post
    And here is what the output should be.
    Please post the output you're getting now, plus the input you're using.

    Second, just to warn you -- floating point calculations when done on a binary-based system such as a computer will not be exact. If you have the right formula, but your output is off by a small amount, then that is the reason for the issue. Your instructor should have pointed that out to you beforehand.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 30th, 2014 at 04:32 AM.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Math Doesn't Match

    Your problem is using floats. And expecting results that make any kind of numeric/financial sense.

    Change to double, and you should be getting results that at least have midly realistic sense.



    wild guess, your calculation of P seems totally off.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Math Doesn't Match

    1) As OReubens mentioned, your calculation of P is incorrect
    (You have L in the denomentor instead of the numerator).

    2) Your ask for the interest rate as a percent (I assume that
    would be a number between 0 and 100). It should be a number
    between 0 and 1.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Math Doesn't Match

    In situations like this, I'll typically break the equation down into individual statements. Makes it easier to understand and to see what's actually going on. A statement like the one you've written is pretty hard to debug as you can't see the individual steps, the order they're performed or the intermediate results.

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Math Doesn't Match

    Quote Originally Posted by Philip Nicoletti View Post
    2) Your ask for the interest rate as a percent (I assume that
    would be a number between 0 and 100). It should be a number
    between 0 and 1.
    It can be 0 to 100, but then you'll have to divide by 100.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Math Doesn't Match

    Yes ... I meant the equation is in a form such that the rate must be between 0 and 1.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured