Irritating algorithm?

Hello!

It's me, working on another homework assignment. I've been at this one for almost 4 hours now and I can't figure it out. Here's the assignment:

A "discount" loan is one where the total interest on the loan is deducted right off the top from the amount of the loan. You effectively pay back the interest on the loan immediately and then pay the principal in equal installments. (The project instructions give a detailed example.) That means if you want $1,000 in hand right away, the face value of the loan must be more than $1,000. Your program calculates the face value of such a loan based on the amount of money you want to get in hand right away. Write a program that will take three inputs: the ammount the consumer needs to recieve, the interest rate, and the duration of the loan in months. The program should then calculate the face value required in order for the consumer to receive the ammount needed. It should also calculate the monthly payment. Your program should allow the calculations to be repeated as often as the user wishes.


I know, stupid idea for a loan, but hey, it's an assignment. Here's what I have done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    float inHand, interestRate, loanMonths, loanYears, faceValue, finalFaceValue, monthlyPayment, totalInterest, test;
    string goAgain;
    
do {
    cout<<"How much money do you need in hand? ";
    cin>> inHand;
    cout<<"What is the interest rate (in decimal form) ";
    cin>> interestRate;
    cout<<"How many months is the loan (in months) ";
    cin>> loanMonths;
    loanYears = (loanMonths / 12);
    
    faceValue = inHand / (1 - interestRate * loanYears); 
    totalInterest = (interestRate * faceValue * loanYears);
    finalFaceValue = faceValue + totalInterest;
    monthlyPayment = (finalFaceValue /loanMonths);
    
    cout<<"\nYou need a loan with a face value of $"<< finalFaceValue <<"\n";
    cout<<"Your monthly payment is $"<< monthlyPayment <<"\n";
    
    cout<<"\nWould you like to try another loan? (Enter yes or no) ";
    cin>> goAgain;
    }
    while (goAgain == "Yes" || goAgain == "yes");
    
    system("PAUSE");
    return 0;


The loop works, the input works, everything works...except the output is wrong.

When given the assignment a screenshot of the finished product was included. The example listed on the screenshot was as follows:

1
2
3
4
5
6
Please enter amount you need in hand: 8000
Please enter the interest rate: 0.05
Please enter the loan period in months: 18

You need a loan with face value equal to $8648.65
Your monthly payment will be $480.48


According to my calculations the formula is as follows:

n=p-rpt
(n being money needed, p being face value, r being rate, and t being duration of the loan in years). The program needs to solve for p. After rearranging the equation I come up with:
p=n/1-rt


This rearranged formula works on paper for the numbers in the screenshot that was given. It does not work in my code. What did I do wrong?
What is your output?
This is the output from my code:

1
2
3
4
5
6
7
8
How much money do you need in hand? 8000
What is the interest rate (in decimal form) .05
How many months is the loan (in months) 18

You need a loan with a face value of $9297.3
Your monthly payment is $516.516

Would you like to try another loan? (Enter yes or no) 
Well, i am not into these types of financial calculations, but the program works (the same output as the example) when you change

1
2
 
finalFaceValue = faceValue + totalInterest;


to

1
2
 
finalFaceValue = faceValue;


What are lines 21 and 22 for, if line 20 already computes the face value according to your equation?

This might not be relevant to the exercise: Isn't interest usually yearly, i.e. rate^time rather than rate*time? Should the interest thus take into account the monthly payments (and the initial reduction at time 0)?
Last edited on
Well, i am not into these types of financial calculations, but the program works (the same output as the example) when you change

1
2

finalFaceValue = faceValue + totalInterest;


to

1
2

finalFaceValue = faceValue;


Ahh! I knew it was just some stupid simple thing! Thank you!
Topic archived. No new replies allowed.