Write a C program that calculates the monthly payment and amortization schedule for a loan. The program should prompt the user to enter the loan amount, the annual interest rate, and the term of the loan (in years). Use the formula below to calculate the monthly payment. Note that the user input for interest rate and term will be in years, but you will need the monthly interest rate and term in months for this formula. main - prompt the user for the loan amount, annual interest rate, and the term of the loan in years. You are required to ensure loan amount and term are both greater than 0. You are required to ensure the annual interest rate is greater than 0 and less than or equal to 30. Use a while loop in each case to continuously prompt the user for a valid value until one is entered. Once all valid data is entered, call computeMonthtyPayment, then printLoanlnfo, and then printTable. You are required to use a do/while loop around code that prompts the user to execute the loan analysis multiple times, each for a different loan, without the user having to re-run your program accepts the loan amount, annual interest rate, and term of the loan in years. This function computes and returns the monthly payment. accepts the loan balance and annual interest rate. This function computes and returns the monthly interest. accepts and prints the basic loan information; specifically the loan amount, monthly loan payment, annual interest rate, monthly interest rate, the term of the loan in years, the term of the loan in months, and the monthly loan payment. Compute whatever values are necessary in this function. accepts the loan payment, the loan amount, the annual interest rate, and the loan term in years. This function prints the amortization table for the loan. Use a for loop to iterate over each month and compute and print the monthly loan information. Compute each column as follows: interest for the month = beginning monthly balance * monthly interest rate principal payment loan balance - Interest new balance - beginning balance - principal payment Display all information as shown on the sample program runs that follow. The amortization table displays the loan balance at the beginning of each month, the monthly payment, the interest for the month, the principal payment, and the new balance. There should be an output line for each month over the life of the loan, for example, if a loan is for 5 years, there will be 60 lines of output. Display all currency with two places to the right of the decimal; display all interest rate values with three places to the right of the decimal.
#include <stdio.h>
#include <math.h>
float computeMontlyPayment(float loan_amount, float rate, int term){
rate /= 12.0; // converting to rate per month
term *= 12; // converting to months
return (rate * loan_amount) / (1 - pow(1 + rate, -term));
}
float computeMonthlyInterest(float loan_balance, float interest_rate){
return loan_balance * interest_rate / 12.0;
}
float printTable(float loan_amount, float rate, int term){
int i;
float loan_balance = loan_amount, interest_for_month, principal_payment, monthly_payment;
for(i = 0; i < 12 * term; ++i){
interest_for_month = computeMonthlyInterest(loan_balance, rate);
monthly_payment = computeMontlyPayment(loan_balance, rate, term);
principal_payment = monthly_payment - interest_for_month;
printf(\"%.2f\\t%.2f\\t%.3f\\t%.2f\\t%.2f\ \", loan_balance, computeMontlyPayment(loan_balance, rate, term), interest_for_month, principal_payment, loan_balance - principal_payment);
loan_balance -= principal_payment;
}
}
int main(){
float loan_amount;
float interest_rate;
int term;
while(1){
printf(\"Enter loan amount: \");
scanf(\"%f\", &loan_amount);
if(loan_amount < 0){
printf(\"Loan amount can\'t be negative. Try again!!\ \");
}
else{
break;
}
}
while(1){
printf(\"Enter annual interest rate: \");
scanf(\"%f\", &interest_rate);
if(interest_rate < 0 || interest_rate > 30){
printf(\"Loan amount can\'t be negative or higher than 30% Try again!!\ \");
}
else{
break;
}
}
while(1){
printf(\"Enter term of loan in years: \");
scanf(\"%d\", &term);
if(term < 0){
printf(\"Term of loan can\'t be negative. Try again!!\ \");
}
else{
break;
}
}
interest_rate /= 100;
printTable(loan_amount, interest_rate, term);
return 0;
}