mortgage calculator

Please help me. I have to make a program that prints out the payment plan for a house. The program should look like this but not necessarily with those numbers:
What is the value left on the mortgage?
10000
What is the annual interest rate of the loan, in percent?
12
What is the monthly payment?
500
Month Payment Amount Owed
1 500.00 9600.00
2 500.00 9196.00
3 500.00 8787.96
...
22 500.00 211.37
23 213.48 0

Calculation explanation: If the annual interest rate is 12%, the monthly rate is 1%. Thus, the interested accrued in the first month is $10000 x .01 = $100 and the loan value is $10,100 right before payment. After the payment, the amount owed is $10100 - $500 = $9600. After the second month, the value owed is $9600 + $9600 x .01 = $9696. After making the payment, $9196.00 is owed. In month 22, $711.37 is owed. After the $500 payment, $211.37 is owed. BUT, after the end of month 23, $213.48 is owed due to the interest on the $211.37 for one month, which is about $2.11. Thus, the last payment has to be $213.48 and not $211.37.

I also must calculate if the monthly payment will pay off the mortgage! If the payment is too low, I have to let the user know right away.

Ex:

What is the value left on the mortgage?
10000
What is the annual interest rate of the loan, in percent?
12
What is the monthly payment?
100
Sorry, this plan won’t work.

Please help me. I know that the only thing I need is an if-else statement and a while loop in the else statement but I am lost as to how to make the loop work properly. I am only allowed to use <stdio.h> and <stdlib.h>. Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>

int main()

{

float payment, mortgage, annual, monthly, after_payment, interest, monthly_interest, interest_accrued;
int month=1;

printf("What is the value left on the mortgage?\n");
scanf("%f", &mortgage);

printf("What is the annual interest rate of the loan, in percent?\n");
scanf("%f", &interest);

printf("What is the monthly payment?\n");
scanf("%f", &payment);

interest = interest/100;
monthly_interest = interest/12;
interest_accrued = (mortgage*monthly_interest) + mortgage;
after_payment = interest_accrued-payment;

if (payment>(mortgage*monthly_interest)+mortgage)
printf("Sorry, this plan won’t work.\n");

else {

while(after_payment>payment){
printf("Month\t Payment\t Amount Owed\n %.2f\t %.2f\t %.2f", month++, payment, after_payment);

}
}

system("pause");
return 0;
}

Please help me.
Last edited on
closed account (48T7M4Gy)
if (payment<(mortgage*monthly interest)+mortgage)

Try that line ie without the "
closed account (48T7M4Gy)
also you appear to have a block of code that needs 'bracing'
else
{
interest = interest/100;
monthly_interest = interest/12;
interest_accrued = (mortgage*monthly_interest) + mortgage;
after_payment = interest_accrued-payment;
}
closed account (48T7M4Gy)
also
while(owed>payment)

where ru getting the idea you need to put the statements in quotes? You don't need to, especially because it's a blooper.

Let us know if things aren't working. Please keep to this particular thread, not for my benefit, just so ppl can see the full history and progress. Makes it easier to see your latest version if you have decided to come back. :)

PS you while is followed by a break statement which guarantees the looping will stop and go no further in that loop. Your loop condition might be better as an if (...) ? as there isn't anything in the loop block to make more than one pass anyway. Maybe you are attempting a schedule of payments printout? In which case you need a time factor, eg monthNo = x and monthNo++ type stuff in the block?
Last edited on
Thank you kemort. That solved my issue where it kept printing "Sorry, this plan won’t work." but it still wont execute the rest of the program.
closed account (48T7M4Gy)
OK just go through the other comments I made about code block etc. You'll get more sense out of your program. The block ensures all the block, not just the direct line after the else gets executed. You might need more prints too, maybe. :)
I have to make a plan of how the payments will go like in the example I typed above. So edited the code based on your comments but I am unsure how to change the while into an if. can you please explain your reasoning.
closed account (48T7M4Gy)
No, I think you need to keep the while for a schedule. But you need to add a few lines to the while block to increment the months, otherwise it will only printout the payemnt for one month. A for loop can do the same thing.

start at month 1
while month is not the last month
{
print payment
add another month
)
Last edited on
ok I think I found some issues and I changed the code but it's now an infinite loop and with alot of zero's lol.
closed account (48T7M4Gy)
All you need to do is fill in where it says blah blah and you'll get the output as shown. And the 'no plan' works too if you select values to give you an impossible mortgage that never reduces. :)

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
#include <stdio.h>
#include <stdlib.h>

int main()

{
    
    float payment, mortgage, annual, monthly, after_payment, interest, monthly_interest, interest_accrued;
    int month = 0;// <---
    
    printf("What is the value left on the mortgage?\n");
    scanf("%f", &mortgage);
    
    printf("What is the annual interest rate of the loan, in percent?\n");
    scanf("%f", &interest);
    
    printf("What is the monthly payment?\n");
    scanf("%f", &payment);
    
    interest = interest/100;
    monthly_interest = interest/12;
    
    while( mortgage >= 0 )// <--
    {
        interest_accrued = mortgage * monthly_interest;// <--
        after_payment = blah blah - payment;// <--
        
        mortgage += blah blah;// <--
        
        if( after_payment blah blah ) {// <--
        
            printf("Sorry, this plan won’t work.\n");
            blah blah;
        }
        
        else
            printf("Month %d Payment %.2f Amount Owed %.2f\n", month, payment, mortgage);// <--
        
        month++;// <--
    }

    return 0;
}


What is the value left on the mortgage?
1000
What is the annual interest rate of the loan, in percent?
5
What is the monthly payment?
167
Month 0 Payment 167.00 Amount Owed 837.17
Month 1 Payment 167.00 Amount Owed 673.65
Month 2 Payment 167.00 Amount Owed 509.46
Month 3 Payment 167.00 Amount Owed 344.58
Month 4 Payment 167.00 Amount Owed 179.02
Month 5 Payment 167.00 Amount Owed 12.77
Month 6 Payment 167.00 Amount Owed -154.18
Program ended with exit code: 0
closed account (48T7M4Gy)
Looks familiar ...

http://www.cplusplus.com/forum/beginner/175118/

Topic archived. No new replies allowed.