I am getting an error on this. what Did I miss?

#include <iostream>
#include <cmath>

using namespace std;

int main()

double L;
double rate;
int n;

// 2 decimal points

// print program title
cout << "Program to calculate Loan Payment" << endl << endl;

cout << "Enter loan amount: ";
cin >> L;
cout << "Enter loan interest rate: ";
cin >> rate;
cout << "Enter number of payments: ";
cin >> n;

// divide rate by 12 for monthly rate
rate = rate / 12;

// calculate payment

double payment = (rate * pow((1+rate),n) /(pow((1+rate),n)-1)) * L;

cout << "Loan amount: $" << L << endl;
cout << "Monthly interest rate: " << rate << "%" << endl;
cout << "Number of payments: " << n << endl;
cout << "Monthly Payment: $" << payment << endl;
cout << "Amount paid back: $" << (payment * n) << endl;
cout << "Interest paid: $" << ((payment * n)-L) << endl;

return 0;

Last edited on
What error where? It would help if you posted the output from the compiler.

The monthly payment on a loan may be calculated by following formula:
Payment = Rate * (1+rate)^n/((1+Rate)^n-1)

Rate is the monthly interest rate, which is the annual interest rate divided by 12.(12% annual interest would be 1 percent monthly interest.) n is the number of payments and L is the amount of loan.
Write a program that asks for these values and displays a report similar to:
Loan amount: $10000.00
Monthly interest rate: 1%
Number of payments: 36
Monthly Payment: $332.14
Amount paid back: $11957.15
Interest paid: $1957.15

my out put comes out
Loan amount: :10000.00
Monthly interest rate:0.08
Number of payments: 36
Monthly Payment: $882.81
Amount paid back: $31781.29
Interest paid: $21871.29

as you can see my out put is wrong because it should comes out like the top one. interest rate is 1% but it shows 0.08%
I dont know why!
.08% looks like you divided 1%/12 rather than 12%/12. I would investigate whether your rate input is coming in as 1 rather than 12.

A suggestion: Don't reuse the value rate. Store annual rate in a separate variable from monthly rate. Then, for the time being, print out both.

Also, when you post code, please place it inside code tags. Click the "<>" button in the Format options and paste your code inside.

Also, your output doesn't match your code. According to your code, the Loan amount should have a "$" in front of it, but your output does not. Please copy and paste rather than re-typing your output, if possible.
Thanks very much for your help.
Topic archived. No new replies allowed.