-
September 29th, 2014, 07:09 PM
#1
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?
-
September 30th, 2014, 01:22 AM
#2
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
-
September 30th, 2014, 04:27 AM
#3
Re: Math Doesn't Match
Originally Posted by NearFruitcake
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.
-
September 30th, 2014, 06:57 AM
#4
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.
-
September 30th, 2014, 12:01 PM
#5
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.
-
September 30th, 2014, 12:16 PM
#6
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.
-
October 1st, 2014, 07:24 AM
#7
Re: Math Doesn't Match
Originally Posted by Philip Nicoletti
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.
-
October 1st, 2014, 07:27 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|