Hello,

You can calculate the annual repayment by using the following equation.

You don't need to do lots of loops as another guy has already said.

First let's define a few variables (all floats):

Premium = Yearly mortgage repayment that you want to find out

LoanAmount = the Amount borrowed

interest = annual rate of interest (you call it APR e.g. 10.5 % would be

entered as 10.5)

term = the length in years that the loan lasts (Best to keep this as a whole

number only)

Now for the equation:

Premium = LoanAmount / FactorA

Where FactorA = [ { 1 - ( ( 100 / ( 100 + interest )) ^ term ) } * 100 /

interest ]

I've tried to use different brackets to make it a bit clearer. Use the same

brackets in

the code.

That's it!

The ^ sign means 'raised to the power of'.

There are more complex formulae for finding the 'true' monthly etc

repayments and also the

capital outstanding and amount of capital repaid in a year and so on. E-mail

me if

you want them. Note that in my example I have set the monthly payments as

one

twelth of annual payments (as banks in the UK do), although this is not

correct it's

usually close enough and it's in the bank's favour!!!.

Also it is best to have a function to calculate FactorA separately from the

main{} function

in your program. Firstly it will keep things tidier and also FactorA is used

quite a lot

in the other equations so it will pay to have a ready made function to

calculate FactorA

on demand.

Here is some code which seems to work to give you the annual premium.

Note there are no error catchers.

---code starts----

#include <stdio.h>

#include <conio.h>

#include <math.h>

float factorA(float, float);

float principal;

float interest;

float term;

float premium;

float factorA_return;

void main(void)

{

printf("Enter the principal\n");

scanf("%f", &principal);

printf("Enter the interest in APR\n");

scanf("%f", &interest);

printf("Enter the term\n");

scanf("%f", &term);

premium = principal / factorA(term, interest);

printf("Annual Premium is %.2f\n",premium);

printf("Monthly Premium is %.2f\n",premium/12);

Quote:

}

float factorA(float term_1, float interest_1)

{

factorA_return = (100.0/(100.0+interest_1));

factorA_return = pow(factorA_return ,term_1);

factorA_return = 1-factorA_return;

factorA_return = factorA_return *100.0/interest_1;

return(factorA_return);

Quote:

}

---code ends----

Best wishes

Ben Davies

London, UK.

www.bendavies.com

Quote:

>This is my beta of my GNU Amortiser Programme v.2.0. I have to add some

>error traps, but if you are in a debate on the affordability of homes in a

>given metro area on a given income, this version will win your argument.

>New features added are the inputs for the auxiliary monthly payments for a

>given home minus utilities. Things like property tax, mortgage insurance,

>and association fees for condos. I'm posting it here for the entertainment

>of C gurus to laugh at a newbie as well as for newbies to learn. :) I does

>lack in beta some more good error traps, but I'll add them soon enough. I

>so far used the source in chi.general to shut up one poster who was

>flaming me with the lame "Get off your arse and WORK" argument. Math is

>real good for shutting up an opponent when you show the work!

>---begin mortgage-2.0.c---

>#include <stdio.h>

>#include <math.h>

>main()

>{

>/* This is the GNU Amortiser Programme coded by {*filter*}y Viking. */

>/* This programme may be distributed under the GNU Public Licence */

>/* for all to enjoy. :) All modifications must fall under the GNU */

>/* Public Licence when distributed. Of course, I coded it in my */

>/* classic BASIC-like novice style. */

>/* This programme was coded by a C novice for other C novices to */

>/* have fun with. :) If you are a C guru, I apologise for my classic */

>/* non-indenting style. After all, I originally learned in BASIC. :) */

>float insurance;

>float principal;

>float monthly;

>float principle;

>float interest;

>float payment;

>float tax;

>float association;

>int duration;

>printf("Enter the principal\n");

>scanf("%f", &principle);

>principal = principle;

>printf("Enter the interest in APR\n");

>scanf("%f", &interest);

>printf("Enter the monthly payment\n");

>scanf("%f", &payment);

>monthly = payment;

>printf("Enter your Tax Multiplier in its percentage format\n");

>scanf("%f", &tax);

>printf("Enter the Association Fee. For single family homes, enter 0\n");

>scanf("%f", &association);

>printf("Enter the mortgage insurance monthly payment\n");

>scanf("%f", &insurance);

>duration = 0;

>interest = interest / 100;

>/* This is the 'juice loan' error detection portion of the programme. */

>/* This part ends the programme in case of a 'juice loan' situation. */

>/* A 'juice loan' is an infinite-length loan. (Chicago slang) */

>if(payment <= ((interest / 12) * principle))

>{

>printf("Infinite Loan Time Error!\n");

>return 0;

>}

>/* This is the negative-interest error trap... */

>if(interest < 0)

>{

>printf("Negative Interest Error!\n");

>return 0;

>}

>/* This is the math formula to amortise the loan in question. */

>/* This loop calculates the number of payments for the 3 variables */

>/* given as you run the executable file after you compile it. */

>while(principle > 0)

>{

>duration = duration + 1;

>principle = principle - (payment - ((interest / 12) * principle));

>printf("Payment No. %d ", duration);

>if(principle > 0)

>{

>printf("Principal remaining is $%.2f\n",principle);

>}

>}

>printf("The Last Payment is $%.2f\n", payment + principle);

>printf("The total paid is $%.2f\n", (payment * (duration - 1)) + payment +

principle);

>printf("\n");

>tax = tax / 12 * principal / 100;

>printf(" $%f is your mortgage payment\n", monthly);

>printf(" $%f is your association fee\n", association);

>printf(" $%f is your monthly property tax\n", tax);

>printf(" $%f is your mortgage insurance payment\n", insurance);

>printf("\n");

>printf(" $%f is your total monthly payment on the home\n", monthly +

association + tax + insurance);

Quote:

>}

>---end source---

>A cool thing I'd like to do is make a backward amortiser that takes the

>interest, principal, and term to deduce the payment. With that, it would

>be cool to code in the monthly_income * .28 to determine the real-life

>payment after it does its thing then printf "Can you REALLY afford it

>after all?" for arguments.

>For that task, becuse I don't know calculus, I will have to code in

>routines that do it by brute force, wasting CPU cycles. For a novice like

>myself, that's fine. For a bank processing lots of mortgages, that

>brute-force approach will not be acceptable. :) A typical PC running Linux

>will do the brute force method nicely, with the way I want to design it.

>I'd design it to first use big incriments for the principal then play

>high-low and decade-down with smaller incriments until the absolute max is

>reached to the penny. The object is to arrive at exactly the term's worth

>of payments and maximise that last one. The initial incriments would be

>count-up to avoid "juice loan" errors. (infinite-loan-time errors)

>--

>CAUTION: Email Spam Killer in use. Leave this line in your reply! 152680

> First Law of Economics: You can't sell product to people without money.

>4410744 bytes of spam mail deleted. http://www.*-*-*.com/ ~nospam/