Problem with current project

I am having trouble getting this to work. I cannot figure out if its a syntax error or what.
Here is the original problem:


Payment = Rate * (1 + Rate)^N / ((1+Rate)^N -1) * Loan

Rate is the monthly interest rate. Rates for loans are
expressed as annual percentage rate (e.g., 18.0% APR).
N is the number of payments.
Loan is the amount of the loan.

Write a program with an interactive console dialog to
accept input from the keyboard. Then calculate the
monthly payment, amount paid back, and the interest paid.
Display a formatted report similar to:

Loan Amount: $ 10000.00
Annual Interest Rate: 12.00%
Number of Payments: 36
Monthly Payment: $ 332.14
Amount Paid Back: $ 11957.15
Interest Paid: $ 1957.15


Here is my code so far.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

int main()
{
    //define variables
    
    double loanamount;
    
    double rate;
    
    double payments;
   
    double monthly;
    
    double payback;
    
    double intrest;
    
    double a;
    
    double b;
    
    double c;
    
    //inputs
    cout<<"Enter Loan Amount: ";
    cin>>loanamount;
    
    
    cout<<"Enter Intrest Rate: ";
    cin>>rate;
    
    cout<<"Enter Number of Payments: ";
    cin>>payments;
    
    system("CLS");
   
    
    //define formula 
    a = rate * (pow((1+rate),payments));
    b = (pow((1+rate),(payments-1)*loanamount));
    monthly = a/b;
    //formula
    
    
   
    //outputs
    cout<<"Loan Amount: "<<loanamount<<endl;
    cout<<"Anual Intrest Rate: "<<rate<<endl;
    cout<<"Number Of payments: "<<payments<<endl;
    cout<<"Monthly Payment: "<<monthly<<endl;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c;
    
    
            
    
    
    
    return 0;
}


The reason there are cout's for A,B,C is just for testing trying to get the numbers to come out right. So far i cannot figure out how to get the pow() function to work correctly. Any help is appreciated! Thanks!

closed account (28poGNh0)
This line (pow((1+rate),(payments-1)*loanamount)); it is not as is shown in the formula
((1+Rate)^N -1) * Loan
maybe you need to turn it to (pow((1+rate),payments)-1)*loanamount;

also you have a lot of unused variables check them

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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

int main()
{
    //define variables

    double loanamount;
    double rate;
    double payments;
    double monthly;
    double a;
    double b;

    //inputs
    cout<<"Enter Loan Amount: ";
    cin>>loanamount;

    cout<<"Enter Intrest Rate: ";
    cin>>rate;

    cout<<"Enter Number of Payments: ";
    cin>>payments;

    system("CLS");

    //define formula    Payment = Rate * (1 + Rate)^N / ((1+Rate)^N -1) * Loan
    a = rate * (pow((1+rate),payments));
    b = (pow((1+rate),payments)-1)*loanamount;
    monthly = a/b;
    //formula

    //outputs
    cout<<"Loan Amount: "<<loanamount<<endl;
    cout<<"Anual Intrest Rate: "<<rate<<endl;
    cout<<"Number Of payments: "<<payments<<endl;
    cout<<"Monthly Payment: "<<monthly<<endl;
    cout<<a<<endl;
    cout<<b<<endl;

    return 0;
}


Hope that helps
Last edited on
The numbers pushed out still do not match the example display given.
Putting 10000 as loan amount.
.12 as intrest rate.
and 36 as monthly payments.

The output is a wacky scientific notation answer?
closed account (28poGNh0)
Thats because you declared those variables as doubles
what are you expecting as number?
It should come out as 332.14 for monthly payments, Not 1.3454E^-64 or whatever.
closed account (28poGNh0)
based on this formula the numbers obtained will be a huge one beleive me

the scientific form for double occurs when the value riches a big number I guess 999999 ,just to simplify

If you want a double value without a scientific notation in this exp turn this lines
1
2
cout<<a<<endl;
cout<<b<<endl;

1
2
cout<< fixed << a << endl;
cout<< fixed << b << endl;

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

int main()
{
    //define variables

    double loanamount;
    double rate;
    double payments;
    double monthly;
    double a;
    double b;
    double c;
    
    //inputs
    cout<<"Enter Loan Amount: ";
    cin>>loanamount;

    cout<<"Enter Intrest Rate: ";
    cin>>rate;

    cout<<"Enter Number of Payments: ";
    cin>>payments;

    system("cls");

    //define formula    Payment = Rate * (1 + Rate)^N / ((1+Rate)^N -1) * Loan
    a = pow((1+rate),payments);
    b = pow((1+rate),payments-1);
    c = rate * a/b *loanamount;
    //monthly = (rate * ((pow(1+rate, payments)))/(pow(rate,payments-1)))*loanamount;
    
    //formula

    //outputs
    cout<<"Loan Amount: "<<loanamount<<endl;
    cout<<"Anual Intrest Rate: "<<rate<<endl;
    cout<<"Number Of payments: "<<payments<<endl;
    cout<<"Monthly Payment: "<<fixed<<c<<endl;
  

    return 0;


This is what i have so far and this is my output

Enter Loan Amount: 10000
Enter Intrest Rate: .12
Enter Number of Payments: 36
sh: cls: command not found
Loan Amount: 10000
Anual Intrest Rate: 0.12
Number Of payments: 36
Monthly Payment: 1344.000000


Thats still not right. I know i can put in a setprecision(2) to get rid of all those zero's but still all around wrong number.
Damn it, this thread is driving me crazy; it keeps popping up in the forum. Here you go, at last:

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double c, r, n;

    cout << "Enter Loan Amount: ";
    cin >> c;

    cout << "Enter Intrest Rate: ";
    cin >> r;
    r /= 1200;

    cout << "Enter Number of Payments: ";
    cin >> n;
    
    double m = (c * r * pow(1 + r, n)) / (pow(1 + r, n) - 1);
    
    cout << fixed << setprecision(2) << "Loan Amount: $" << c << endl;
    cout << fixed << setprecision(2) << "Annual Interest Rate: " << r * 1200 << '%' << endl;
    cout << fixed << "Number of Payments: " << n << endl;
    cout << fixed << setprecision(2) << "Monthly Payment: $" << m << endl;
    cout << fixed << setprecision(2) << "Amount Paid Back: $" << m * n << endl;
    cout << fixed << setprecision(2) << "Interest Paid: $" << m * n - c << endl;
    
    return 0;
}

Inputting 10000, 12 and 36 yields your desired result.

Go away!
Topic archived. No new replies allowed.