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

double monthlyPayment(double principal, double monthlyInterestRate, int numPayments);


int main(int argc, char *argv[])
{
  if(argc != 4)
  {
     printf("Usage: %s minInterestRate maxInterestRate loanAmount\n");
     exit(-1);
  }
  double minInterestRate = atof(argv[1]);
  double maxInterestRate = atof(argv[2]);
  double loanAmount = atof(argv[3]);

  FILE *output = fopen("result.txt", "w");  
  if(output == NULL)
  {
    printf("Could not open result.txt, quitting...\n");
    exit(-1);
  }
  fprintf(output, "\t\tLoan Amount: $%.2f", loanAmount);
  fprintf(output, "APR Duration Monthly Total\n\n");
  double i, temp;
  int j;
  for(i=minInterestRate; i<=maxInterestRate; i+=0.25)
  {
     for(j=20; j<=30; j+=5)
     {
	temp = monthlyPayment(loanAmount, i/1200.0, j*12);
	fprintf(output, "%.2f\t %d\t $%.2f\t $%.2f\n", i, j, temp, j*12*temp);
     }
  }
  fclose(output);
  
}


double monthlyPayment(double principal, double monthlyInterestRate, int numPayments)
{
  double pay = (monthlyInterestRate * principal) / (1.0 - pow(1+monthlyInterestRate, 0-(double)numPayments));
  return pay;
}
