Explanation:
Code with comments:
#include <stdio.h>
#include <math.h>
/* math library is required to use pow() function
First we need to create a function to calculate the Monthly Payment
It would take three inputs Rate, Years, and Principle amount
Then we converted the Rate and Months according to problem statement
pow() function is used to find (1+R)^M
Then finaly this function returns the value of Monthly Payment */
float MonthlyPay(float R, int M, int P)
{
R=R/1200;
M=M*12;
float po=pow((1+R),M);
float PayM=(R+(R/(po-1)))*P;
return PayM;
}
/* In the main() function we get 3 inputs from the user Years, Rate and Principle amount
then we call the MonthlyPay function and pass it 3 inputs that we got from the user
then we calculated the total Payment by multiplying the Monthly Pay and total number of months
then we calculated the interest Amount by subtracting the Principle Amount from total Pay
Testing is performed and output result is given at the end and also attached in the image */
int main(void)
{
float Rate;
int Year, Amount;
printf("Enter Years: ");
scanf("%d",&Year);
printf("Enter Rate: ");
scanf("%f",&Rate);
printf("Enter Principle Amount: ");
scanf("%d",&Amount);
float Pay = MonthlyPay(Rate, Year, Amount);
printf ("Monthly Payment is: %f \n", Pay);
float totalPay=Pay*Year*12;
printf ("Total Payment is: %f \n", totalPay);
float interest=totalPay-Amount;
printf ("Interest Expense is: %f \n", interest);
return 0;
}
Output:
Enter Years: 5
Enter Rate: 7
Enter Principle Amount: 12200
Monthly Payment is: 241.572767
Total Payment is: 14494.367188
Interest Expense is: 2294.367188