/*
 * This program will calculate a monthly payment.
 * Author: David Janzen
 */
#include <stdio.h>
#include <math.h>
#include "checkit.h"

double roundToTwoDecimals(double num)
{
     return (int)(num * 100 + .5) / 100.0;
}

double calcPayment(int principle, int term, double rate)
{
     double monthlyRate;
     double payment;

     monthlyRate = rate / 12;
     payment = (monthlyRate * principle) / (1 - pow(1 + monthlyRate, -1 * term));
     return roundToTwoDecimals(payment);
}

void test_cases(void)
{
     checkit_double(478.92, calcPayment(20000, 48, .07));
     checkit_double(536.82, calcPayment(100000, 360, .05));

     checkit_double(23.95, roundToTwoDecimals(23.94856));
}

int main(void)
{
     int principle;
     int term;
     double rate;

     test_cases();
     printf("Please enter the principle: ");
     scanf(" %d", &principle);

     printf("Please enter the term: ");
     scanf(" %d", &term);

     printf("Please enter the rate: ");
     scanf(" %lf", &rate);

     printf("Your monthly payment is: $%.2f\n", calcPayment(principle, term, rate));
     
     return 0;
}