Financial Application (Comparing loans with various interest rates

Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8
Here is sample run:

Loan Amount: 10000
Number of years: 5

Interest rate Monthly Payment Total payment
5% 188.71 11322.74
5.125% 189.28 11357.13
5.25% 189.85 11391.59
……
7.85% 202.16 12129.97
8.0% 202.76 12165.83

This is my program that 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;

int main()
{
double loanAmount;
int numberOfYears;
double annualInterestRate = 5;
double monthlyInterestRate;
double monthlyPayment;
double totalPayment;

// Enter loan amount
cout << "Loan Amount: ";
cin >> loanAmount;

// Enter number of years
cout << "Number of Years: ";
cin >> numberOfYears;

// Obtain monthly interest rate
monthlyInterestRate = annualInterestRate / 12;

int i = annualInterestRate;
for (i = 5; i <= 8; i += 0.125)
{

int j;
for(j = numberOfYears; j <= numberOfYears;j++)
{
double monthlyPayment = monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
cout << setw(3) << j;
cout << "\n";
}

int x;
{
for(x = numberOfYears; x < totalPayment; x++ )
double totalPayment = monthlyPayment * numberOfYears * 12;
cout << setw(3) << x;
cout << "\n";
}
}

// Display results
cout << "Interest Rate " << annualInterestRate << "% " << "\nMonthly Payment " << monthlyPayment << "\nTotal Payment " << totalPayment << endl;

return 0;
}
[code]
[/code]
Last edited on
Did you have a question? You didn't ask one.

From what you've posted, your program won't do what the instructions say.

1
2
int i = annualInterestRate;
for (i = 5; i <= 8; i += 0.125)

Why are you setting i in the first statement, then overwriting in the next statement?

The for loop won't do what you want. i is an int. You can't add 0.125 to an int. Well, you can, but the result doesn't change since the value is truncated to 0.

 
for(j = numberOfYears; j <= numberOfYears;j++)

This loop will execute eactly once. Is that what you want?

 
for(x = numberOfYears; x < totalPayment; x++ )

Does x represent years or payments? You're mixing the two.

totalPayment is uninitialized.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Last edited on
Please use code tags when posting code. Highlight the code and then click the <> button on the right of the edit window. Also, please ask specific questions. For a program like this, you'd probably want to give the input, the expected output, and the actual output. Doing these things will greatly increase the chances of getting helpful comments.

Here is your tagged code. See my comments below it.
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
#include <iostream>
#include <cmath>
#include<iomanip>
using namespace std;

int main()
{
double loanAmount;
int numberOfYears;
double annualInterestRate = 5;
double monthlyInterestRate;
double monthlyPayment;
double totalPayment;

// Enter loan amount
cout << "Loan Amount: ";
cin >> loanAmount;

// Enter number of years
cout << "Number of Years: ";
cin >> numberOfYears;

// Obtain monthly interest rate
monthlyInterestRate = annualInterestRate / 12;

int i = annualInterestRate;
for (i = 5; i <= 8; i += 0.125)
{

int j;
for(j = numberOfYears; j <= numberOfYears;j++)
{
double monthlyPayment = monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
cout << setw(3) << j;
cout << "\n";
}

int x;
{
for(x = numberOfYears; x < totalPayment; x++ )
double totalPayment = monthlyPayment * numberOfYears * 12;
cout << setw(3) << x;
cout << "\n";
}
}

// Display results
cout << "Interest Rate " << annualInterestRate << "% " << "\nMonthly Payment " << monthlyPayment << "\nTotal Payment " << totalPayment << endl;

return 0;
}

Line 26: since I can take on non-integer values, it should be declared as a double.

Line 31: this loop will execute only once. After one execution, j is incremented to numberOfYears+1 and that causes the test to fail. Did you mean to start j at 0?

Line 40: totalPayment is uninitialized the first time through the loop. The second time through, it will probably be larger than totalPayment, unless the loan is for a very small amount of money, so this loop executes 0 or 1 times usually. What is the purpose of the loop? Don't you want to execute this only once? In that case you don't need a loop at all.
sorry ,but i still confuse with the code .
Is this what u mean ?

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

int main()
{
    double loanAmount;
    int numberOfYears;
    double annualInterestRate = 5;
    double monthlyInterestRate;
    double monthlyPayment;
    double totalPayment;

    // Enter loan amount
    cout << "Loan Amount: ";
    cin >> loanAmount;

    // Enter number of years
    cout << "Number of Years: ";
    cin >> numberOfYears;

    // Obtain monthly interest rate
    monthlyInterestRate = annualInterestRate / 12;

    for (double i = 5; i <= 8; i += 0.125)  // i has to be a float
    {
        for(double j = 1; j <= numberOfYears;j++)
        {
            monthlyPayment = monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
            cout << setw(3) << j;
            cout << "\n";
        }
    }

    for (double x = numberOfYears; x < totalPayment; x++ )
    {
        totalPayment = monthlyPayment * numberOfYears * 12;
        cout << setw(3) << x;
        cout << "\n";
    }

    // Display results
    cout << "Interest Rate " << annualInterestRate << "% " << "\nMonthly Payment " << monthlyPayment << "\nTotal Payment " << totalPayment << endl;

    return 0;
}[/code]
Line 36 still makes no sense. You're mixing years and amounts.
Topic archived. No new replies allowed.