Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use C Programming not C++ Develop a code that calculates and prints data

ID: 3825872 • Letter: P

Question

Please use C Programming not C++

Develop a code that calculates and prints data in designated formats. The annual (or regular interval) payment w from a lump sum P with a compound interest r is referred to as annuity. Assuming that the sum is completely withdrawn in n years, w, and the total of the payments W_i in the ith year are calculated by the formulas, w = P(r(1 + r)^n-1/(1 + r)^n -1) and W_i = i * P * r * (1 + r)^n-1/((1 + r)^n - 1) where n is the number of years over which the annuity occurs. Write a C code that inputs the parameters: P, r, and n; and calculates and outputs W_i for each year.

Explanation / Answer

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

double calcW(double p, double r, int n) {
   double tmp;
   double w;

   tmp = pow(1+r, n-1);
   w = p*r*tmp/(tmp*(1+r) - 1);
   return w;
}


double *calcWi(double p, double r, int n) {
   double *wi = malloc(sizeof(double)*n);
   double w = calcW(p,r,n);
   int i;
   for (i = 1; i<=n; i++)
       wi[i-1] = i*w;
   return wi;
}

void main() {
   double p, r, *wi;
   int n, i;
   printf("Enter p: ");
   scanf("%lf", &p);
   printf("Enter r: ");
   scanf("%lf", &r);
   printf("Enter n: ");
   scanf("%d", &n);
   wi = calcWi(p,r,n);
   printf ("i Wi "   );
   for (i = 1; i<=n; i++)
       printf("%d %.2lf ", i, wi[i-1]);
}

You have the code. I haven't commented the code since the code is very simple. If you have any queries or want me to comment the code, please feel free to comment below. I shall be glad to help you.