#include
#include
#include
int main()
{
char option;
int ch;
float principal, rate, r, duration;
double payment, x, y;
option = ' ';
while (option != 'Q' && option != 'q')
{
clrscr();
printf("%s%s%s%s%s%s%s",
" LOAN CALCULATOR\n\n",
" Choose one of the following four options:\n\n",
" (P) Compute the amount of the loan (P)ayment.\n",
" (A) Compute the loan (A)mount.\n",
" (D) Compute the (D)uration of the loan.\n",
" (Q) (Q)uit this program.\n\n",
" Enter the appropriate letter here: ");
scanf("\n%c", &option);
switch (option)
{
case 'P': case 'p':
clrscr();
while (1)
{
printf("%s%s",
"\n\n\nTo compute the amount of the loan payment, ",
"input the following information:\n\n");
printf("Enter the total amount of the loan:");
scanf("%f", &principal);
printf("Enter the annual interest rate:");
scanf("%f", &rate);
printf("Enter the number of years to repay the loan:");
scanf("%f", &duration);
r = rate / 100;
x = 1 + r / 12;
y = duration * 12;
payment = (principal * r / 12) / (1 - (1 / pow(x,y)));
printf("\n\nYour monthly payment will be: $%.2f\n", payment);
printf("\nWould you like to compute another loan payment (y or n)?");
getchar();
ch = getchar();
if (ch == 'n' || ch == 'N')
break;
}
break;
case 'A': case 'a':
clrscr();
while (1)
{
printf("%s%s",
"\n\n\nTo compute the total loan amount, ",
"input the following information:\n\n");
printf("Enter the preferred payment amount:");
scanf("%f", &payment);
printf("Enter the annual interest rate:");
scanf("%f", &rate);
printf("Enter the number of years to repay the loan:");
scanf("%f", &duration);
r = rate / 100;
x = 1 + r;
y = duration * 12;
principal = (payment * r) * (1 - (1 / pow(x,y)));
printf("\n\nYour total loan amount will be: $%.2f\n", principal);
printf("\nWould you like to compute another loan amount (y or n)?");
getchar();
ch = getchar();
if (ch == 'n' || ch == 'N')
break;
}
break;
case 'D': case 'd':
clrscr();
while (1)
{
printf("%s%s",
"\n\n\nTo compute the duration of a loan, ",
"input the following information:\n\n");
printf("Enter the total amount of the loan:");
scanf("%f", &principal);
printf("Enter the monthly loan payment:");
scanf("%f", &payment);
printf("Enter the annual interest rate:");
scanf("%f", &rate);
r = rate / 100 / 12;
x = payment - r * principal;
y = r + 1;
duration = (log(payment) - log(x)) / (log(y)) / 12;
printf("\n\nThe duration of your loan will be: %f years\n", duration);
printf("\nWould you like to compute another loan duration (y or n)?");
getchar();
ch = getchar();
if (ch == 'n' || ch == 'N')
break;
}
break;
case 'Q': case 'q':
break;
}
}
getch();
return 0;
}