Implement this C program using at least three arrays to store the interest, principle and balance after each payment respectively. For example, interest[0] stores the paid interest in the first payment. Besides, please use three pointers to visit the element in the three arrays separately. Name this C program as loanCalcPtr.c.

Respuesta :

Answer:

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

int main()

{

   float principalAmount, annualInterstRate, monthlyInterestRate;

   float equatedMonthlyInstallment , initialBalance, interest, principalPaid, finalBalance;

   int totalMonths, month;

   float interestList[30], principalPaidList[30], finalBalanceList[30];

   printf(" principalAmount: ");

   scanf("%f", &principalAmount);

  printf(" Annual Interest rate: ");

   scanf("%f", &annualInterstRate);

  printf(" Term in month: ");

   scanf("%d", &totalMonths);

  monthlyInterestRate = (float)(annualInterstRate / (12*100.0));

  equatedMonthlyInstallment  = principalAmount * (monthlyInterestRate / (1 - pow( (1 + monthlyInterestRate) , (-totalMonths) )));

  initialBalance = principalAmount;

  printf(" Monthly Payment: $%.2f ", equatedMonthlyInstallment );

   for(month = 1; month <= totalMonths; month++)

   {

      *(interestList+month-1) = (initialBalance * monthlyInterestRate);

      *(principalPaidList+month-1) = equatedMonthlyInstallment  - *(interestList+month-1);

      *(finalBalanceList+month-1) = (initialBalance - *(principalPaidList+month-1));

      initialBalance = *(finalBalanceList+month-1);

   }

  printf(" %10s %14s %10s %10s ", " Payment ", " principalAmount ", " Interest ", " Balance ");

   for(month = 1; month <= totalMonths; month++)

   {

      printf(" %6d $%-10.2f $%-10.2f $%-12.2f ", month, *(principalPaidList+month-1), *(interestList+month-1), *(finalBalanceList+month-1));

   }

   printf(" Final Payment: $%.2f ", equatedMonthlyInstallment );

  printf(" ");

   return 0;

}

Explanation:

  • Get the information about principal amount, interest rate,  total months  as an input from user.
  • Calculate equated monthly installment  and display monthly payment.
  • Use relevant formulas to calculate loan schedule , interest, principal amount  and final balance  accordingly.
  • Update initial balance with final balance  and display headers , final payment and final payment .