Code:
#include<stdio.h>
float getbalance(void)
{
float balance;
printf("Enter balance.\n");
scanf("%f", &balance);
return(balance);
}
float getrate(void)
{
float apr, rate;
printf("Enter the interest (APR) in percent.\n");
scanf("%f", &apr);
rate = apr / 1200;
return(rate);
}
int getterm(void)
{
int term;
printf("Enter the term in months.\n");
scanf("%d", &term);
return(term);
}
float getpayment(void)
{
float payment_fixed;
printf("Enter the payment amount (£).\n");
scanf("%f", &payment_fixed);
return(payment_fixed);
}
int iterate(float balance, float rate, float minpay, float payment_fixed, int silent)
{
float interest, payment;
float total_interest = 1;
int month = 1;
while (balance > 0)
{
interest = rate * balance;
if(interest <= 0.5)
if(rate == 0)
interest = 0;
else
interest = 0.5;
payment = ((minpay / 100) * balance) + payment_fixed;
balance = balance + interest;
if(payment <= 5)
payment = 5;
if(balance <= payment)
payment = balance;
balance = balance - payment;
if(silent != 0)
printf("%d\t£%.2f\t\t£%.2f\t\t£%.2f\n", month, balance, interest, payment);
month++;
total_interest += interest;
}
if(silent != 0)
{
printf("Term = %d months.\n", (month -1));
printf("Total interest = £%.2f\n", total_interest);
}
return((month - 1));
}
float payment(float balance, float rate, int term)
{
float pvif;
int i;
pvif = (1 + rate);
for(i = 0; i < term; i++)
{
pvif = pvif * (1 + rate);
}
return((balance * pvif) / ((pvif - 1) / rate));
}
int main()
{
float balance, rate, minpay, payment_fixed;
int term, selection, silent;
float guess, value, guess_p;
// Global variables:
printf("Choose payment type:\n");
printf("\t1: Minimum payment (credit card)\n");
printf("\t2: Fixed payment (variable term)\n");
printf("\t3: Fixed Term\n");
printf("\t4: Calculate APR from fixed payment and term\n");
printf("Selection: ");
scanf("%d", &selection);
switch (selection) {
case 1 :
// get balance, rate, minimum payment amount.
balance = getbalance();
rate = getrate();
// Get minimum payment amount in percent
printf("Enter the minimum payment amount in percent.\n");
scanf("%f", &minpay);
iterate(balance, rate, minpay, 0, 1);
break;
case 2 :
// get balance, rate, fixed payment amount.
balance = getbalance();
rate = getrate();
payment_fixed = getpayment();
iterate(balance, rate, 0, payment_fixed, 1);
break;
case 3 :
//get balance, rate, term.
balance = getbalance();
rate = getrate();
term = getterm();
printf("Payment = £%.2f\n", payment(balance, rate, term));
break;
case 4 :
// get balance, term, payment.
balance = getbalance();
term = getterm();
payment_fixed = getpayment();
silent = 0;
// starting guess
guess = 10.0;
value = 1;
while (value > 0.001)
{
if(iterate(balance, (guess / 1200), 0, payment_fixed, 0) < term)
{
guess_p = guess;
guess = guess + value;
}
else
{
guess = guess_p;
value = value / 10;
}
}
printf("APR = %.2f\%\n", guess);
break;
default :
printf("Bad input, Quitting!\n");
break;
}
return 0;
}
Its not totally accurate and i'm still improving it. Just looking for any insight on the way i'm doing things. Sorry about the lack of comments.