Question About Elementary Programming

I'm doing exercises in my intro to C++ book and I am a little baffled as to why the code is supposed to be the way it is.
I did the example my way and the example data entered produced different results then the books example data results.

So I rewrote it the books way and it works but they don't provide an explanation as to why and I can't find anything on google.


The part that confuses me is this line:
monthlyRate = interestRate / 1200;

At first I had it as monthlyRate = interestRate / 12;
But that produced bad results.
Why does it have to be 1200 instead of 12 ?





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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 double interestRate;
 int years;
 double loanAmount;
 double monthlyRate;
 double monthlyPayment;
 double totalPayment;

	cout << "Enter the annual interest rate: ";
	cin >> interestRate;

	cout << "\n Enter the number of years: ";
	cin >> years;

	cout << "\n Enter the loan amount: ";
	cin >> loanAmount;

	monthlyRate = interestRate / 1200;

	monthlyPayment = (loanAmount * monthlyRate) / (1 - (1 / (pow(1 + monthlyRate, years * 12))));

	totalPayment = (monthlyPayment * 12) * years;

	cout << "Your monthly Payment is: " << monthlyPayment << endl << "Your total payment is: " << totalPayment;

	return 0;
}


I am guessing you are supposed to enter in the interestRate as a percentage, so the program divides by 1200 to account for both the number of months and the percentage. (Remember n% ~ n/100)
Later on, the program uses monthlyRate in some calculations, so having the decimal form and not the percentage is important.
Topic archived. No new replies allowed.