#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    C++ Code Difficulty Programming an Equation


    Hey everyone, I'm having difficulty solving a problem in class regarding the calculation of the monthly payment on a loan. I'm using C++ to do so and have come to a point I can't seem to get past.
    It asks to calculate it using the following formula:
    Payment= (Rate*(1+Rate)^N)/(((1+Rate)^N-1) * L
    Rate= monthly interest rate (1.0%)
    N= number of payment (36)
    L= amount of the loan ($10000.00)
    And the monthly payment should be $1957.15

    I've tried to program it but it wont even launch :confused:
    Here's what I have so far, any input would be really helpful! trying to learn this before it's time for my test.

    //This program will calculate the monthly payment on a loan.
    #include <iostream>
    using namespace std;

    int main ()
    {
    double interestRate;
    int numPayments;
    double loanAmount;
    double monthlyPayment;

    cout << "Please enter interest rate: ";
    cin >> interestRate;

    cout << "Please enter numPayments: ";
    cin >> numPayments;

    cout << "Please enter loanAmount: ";
    cin >> loanAmount;

    monthlyPayment = (interestRate*(1+interestRate)^numPayments)/(((1+interestRate)^numPayments-1)*loanAmount;

    cout << "The monthly payment on the loan is: " << monthlyPayment << "\n";
    system("Pause");

    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    1. Use code tags! It may seem a minor point here, but deliberately making your code unreadable will not motivate us to help you more, but rather immensely less.

    2. What do you mean by "it wont even launch"?
    Does it compile or not?
    Does it compile, but fails to build?
    Does it build but crashes when you try to run it?

    Being deliberately vague and misleading about your problem will have similar demotivating effects on us.

    And if you get error and/or warning messages, shouldn't you tell us what they are, instead of making us play mind-reading guessing games? We cannot help you unless you help us to help you.

    3. What do you think that (1+interestRate)^numPayments does? ^ is the bit-wise XOR operator. I thought that that kind of formula would be raising the interest rate by a power, not exclusive-ORing them. And XORing an int to a floating-point type is exceptionally weird. ^ won't even compile when paired with a floating-point and should generate error messages.

    If you are intending to raise a value to a power, investigate the pow() function.

    And having mis-matched parentheses will also generate an error message, one that should tell you that you have mismatched parentheses.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    1. Use code tags! It may seem a minor point here, but deliberately making your code unreadable will not motivate us to help you more, but rather immensely less.

    2. What do you mean by "it wont even launch"?
    Does it compile or not?
    Does it compile, but fails to build?
    Does it build but crashes when you try to run it?

    Being deliberately vague and misleading about your problem will have similar demotivating effects on us.

    And if you get error and/or warning messages, shouldn't you tell us what they are, instead of making us play mind-reading guessing games? We cannot help you unless you help us to help you.

    3. What do you think that (1+interestRate)^numPayments does? ^ is the bit-wise XOR operator. I thought that that kind of formula would be raising the interest rate by a power, not exclusive-ORing them. And XORing an int to a floating-point type is exceptionally weird. ^ won't even compile when paired with a floating-point and should generate error messages.

    If you are intending to raise a value to a power, investigate the pow() function.

    And having mis-matched parentheses will also generate an error message, one that should tell you that you have mismatched parentheses.
    Ah yea, that, forgot to add that info.
    1.
    Code:
    //This program will calculate the monthly payment on a loan.
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	double interestRate;
    	int numPayments;
    	double loanAmount;
    	double monthlyPayment;
    
    	cout << "Please enter interest rate: ";
    	cin >> interestRate;
    
    	cout << "Please enter numPayments: ";
    	cin >> numPayments;
    
    	cout << "Please enter loanAmount: ";
    	cin >> loanAmount;
    
    	monthlyPayment = (interestRate*(1+interestRate)^numPayments)/(((1+interestRate)^numPayments-1)*loanAmount);
    
    	cout << "The monthly payment on the loan is: " << monthlyPayment << "\n";
    	system("Pause");
    
    	return 0;
    }
    there we go, didn't notice that function earlier.

    2. What I meant is that it doesn't "build", it tells me that there were the following build errors: error C2296: '^' : illegal, left operand has type 'double' (twice), though I fixed that other parenthesis error you mentioned.

    3. I tried that formula that way since I just simply do not know how to put the equation like it asks on paper onto the code:
    payment= Rate * (1+ Rate)^N/((1 + Rate)^N * L

    Now, after trying the pow function this is what I got, also after tweaking the equation a bit trying to get it to work:

    Code:
    // This program will calculate the monthly payment on a loan.
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	double interestRate;
    	int numPayments;
    	double loanAmount;
    	double monthlyPayment;
    	double equationUp;
    	double equationDown;
    
    	cout << "Please enter interest rate: ";
    	cin >> interestRate;
    	
    	cout << "Please enter numPayments: ";
    	cin >> numPayments;
    
    	cout << "Please enter loanAmount: ";
    	cin >> loanAmount;
    
    	equationUp = interestRate*pow(1+interestRate, numPayments)*loanAmount;
    	equationDown = pow(1+interestRate, numPayments)-1;
    	monthlyPayment = equationUp/equationDown;
    	
    	cout << "The monthly payment on the loan is: " << monthlyPayment << "\n";
    	system("Pause");
    
    	return 0;
    }
    The build was successful and it launched but the problem this time was that it did not provide that correct answer that it was supposed to (1957.15), instead it gave me an answer of 332.143.
    So in the end, I got it to work but now I think I'm just getting the equation part wrong entirely.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Have you tried using the formula by hand? Working simple examples by hand is often used to develop and test one's algorithm, as well as for verifying that the formula is correct.

    For one thing, the formula as you presented it also has mismatched parentheses (it's missing a close parenthesis). I think your next step is to verify that you have the formula right (which, on the face of it, you don't as evidenced by the parentheses mismatch).
  8. #5
  9. um, Hello?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    FN23fc
    Posts
    719
    Rep Power
    160
    Are you entering your interest rate as a decimal, like .05 instead of 5?
    "America's abundance was created not by public sacrifices to "the common good," but by the productive genius
    of free men who pursued their own personal interests and the making of their own private fortunes. They did not
    starve the people to pay for America's industrialization. They gave the people better jobs, higher wages and
    cheaper goods with every new machine they invented, with every scientific discovery or technological advance --
    and thus the whole country was moving forward and profiting, not suffering, every step of the way."
    --Ayn Rand
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Having built his code (correcting, I believe, for the mismatched paren), I entered his test values and got the same wrong result he did by entering 0.01 for 1%.

    I also made a further "correction" and fared no better.
    Last edited by dwise1_aol; February 28th, 2011 at 11:28 AM.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I looked up the formula in Wikipedia (http://en.wikipedia.org/wiki/Fixed_r...ayment_formula) and got a simplified form:
    c = P0 * r / (1 - (1+r)**(-N) )
    where
    c = periodic payment
    P0 = initial principal
    r = rate
    N = number of payments
    ** is the FORTRAN notation for raising to a power (in order to avoid confusion with the C XOR bit-wise operator)
    We find this is identical to the OP's version of the formula if we multiply the Wikipedia formula by one (ie, by (1+r)**N / (1+r)**N ). Therefore, the formula checks out (if in the OP's presentation we eliminate an extra open paren immediately after the division operator).

    Plugging the test inputs into the simpler form of the formula obtained from Wikipedia, I still get a monthly payment of 332.14.

    I believe that the test case's expected output value is in error. If we think about it, isn't a monthly payment of $1957.15 for a 3-year loan of $10,000 at 1% rather high? With no interest, that would pay it off in just over 5 payments, so with such a low interest rate, how could it take many more payments? Eg:
    Code:
    Pmt #  Pmt     Principal    Int     Balance
                                       10000
    1    1957.15    1857.15    100      8242.85
    2    1957.15    1874.72    82.43    6450.56
    3    1957.15    1892.64    64.51    4622.42
    4    1957.15    1910.93    46.22    2757.72
    5    1957.15    1929.57    27.58    855.72
    6    1957.15    1948.59    8.56    -1084.31
    Exhumedlight, what I just did is called a sanity check. It's used to quickly check whether something is wildly wrong.

    With a payment of $332.14, the total cost of the loan would be $11,957.15, meaning that $1957.15 would be the total interest paid on that loan. Which coinicidentally is the amount of the OP's expected payment. Curious, that.
    Last edited by dwise1_aol; February 28th, 2011 at 11:31 AM.
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Yeah, I think 1957.15 is the total interest.
    Code:
    Total interest = (monthly_payment * number_of_payments) - initial_loan_amount
    
    Total interest = (332.143 * 46) - 10000 = 11957.148 - 10000 = 1957.148
    Incidentally, I also had to #include <cmath> for the code to compile.
    Last edited by Scorpions4ever; February 28th, 2011 at 05:18 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by Scorpions4ever
    Incidentally, I also had to #include <cmath> for the code to compile.
    Ditto here. Plus some other header file needs to be #include'd for the call to system(). I just commented that line out, since I ran it from the command line as God had intended.
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    ^^^
    Yes, you need to include <cstdlib> for the call to system() to compile correctly. Incidentally, I nuked that line myself too, as I was running from cmd line as well (albeit on a Linux box)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Monthly Payment Program


    --
    Last edited by ericcooper; September 9th, 2013 at 11:02 AM. Reason: N/A
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222

    Night of the Living Dead Thread!


    Really, Eric, don't you think that zombies had been done to death long ago?

    Your first post here responded to a thread from more than two years ago. And if you look more closely, you will see that Exhumedlight, the person to whom you are responding, only posted here twice, both times in this thread that he had created. The last time that he even looked here was around 27 February 2011.

    Still, I hope you stick around here. You do appear to want to help.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    --
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by ericcooper
    First off I'd like to say I am new to programming.

    Second I am new to 'forums':

    I did realize the date - its pretty clear. However, this problem inquired in this thread, (posted before "zombies were bone dead" or whatever analogy you utilized to impolitely reply to my response) was the 3rd homework assignment given to my class by my Professor. I figure if it is 2013 and this problem is still floating around, maybe a fresh reply to this thread could help students coming up and learning about 'pow' functions, and using formulas in their code.
    This question was "floating around" in 2011. It was not "floating around" in 2013, but rather it was available as a Google hit in the present. Certainly, many of us become aware of a particular forum through a particular hit while we are doing a particular hit. That does not make those hits "current".

    If this did indeed relate to your current school assignment, then why did you not say so? Your withholding such information is very impolite! Why did you respond to a long-dead thread? Some honest information would have been in order. Withholding any such information is not in order!

    It is indeed 2013. This question is not still floating around, given this 2011 thread, so your entire raison d'Ítre for your response simply does not exist.

    Do please lose your piss-poor attitude. And, yes, zombies have been done to death (homage to the episode, "200", of StarGate SG-1).

    So, do you have anything to offer here, outside of "me-too!"?

IMN logo majestic logo threadwatch logo seochat tools logo