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

Develop a code that calculates and prints data in designated formats. The annual

ID: 3581949 • Letter: D

Question

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)^r - 1/(1 + r)^r - 1) and W_i = i * P * r (1 + r)^n - 1/((1 + r)^r - 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, for each year. In main Create a structure that contains the following information: name, social security #, Total_years, interest rate, Principal, float arrays W Let Total_years be 5, Principal be 400000, interest rate be 5%. Give a name and social security Call function Annuity with structure as argument Print the data in the following formats: Name: Total years: Interest Rate: Principal: Annual withdrawal Save the above data to a file, "annuity.data", using the above format. Function Annuity Take the structure from the above Write a loop to calculate W_i and store it in the array of the structure

Explanation / Answer

#include<stdio.h>
#include<math.h>
#include<string.h>
//define MAX record
#define MAX 200
typedef struct
{
   char name[20];
   char social_security[30];
   int p;
   float r;
   int n;
   float array_w[MAX];
}Account;

void Annuity(Account *accPtr);
void printAndSave2File(Account *accPtr);

int main()
{
   Account account;
   //declare local variables
   int P=0,n;
   float r;
     
   char name[20];
   char security[20];
   //to keep number of accounts
   int count = 0;
  
   printf("Enter Name: ");
   scanf("%s", name);
   strcpy(account.name, name);

   printf("Social security: ");
   scanf("%s", security);
   strcpy(account.social_security, security);

   printf("Enter principle P: ");
   scanf("%d",&P);
   account.p = P;

   printf("Enter interest rate r: ");
   scanf("%f", &r);
   account.r = r/100;

   printf("Total years: ");
   scanf("%d", &n);
   account.n = n;
  
   //call annuity function
   Annuity(&account);
   printAndSave2File(&account);
  
}

void Annuity(Account *accPtr)
{
   int i;
   float numerator, denominator;
   for (i = 0; i <= accPtr->n; i++)
   {
       //use given formula to calculate wi
       numerator = pow(long double(1 + accPtr->r), (accPtr->n - 1));
       denominator =pow(long double(1 + accPtr->r), accPtr->n) - 1;
       accPtr->array_w[i] = (i + 1)* accPtr->p * accPtr->r * numerator / denominator;
      
   }
}

void printAndSave2File(Account *accPtr)
{
   FILE *in;
   int i;

   in = fopen("annuity.data", "w");

   if (!in)
   {
       printf("Not able to open file ");
       return;
   }

   for (i = 0; i < accPtr->n; i++)
   {
       printf("Year Total payments ");
       fprintf(in,"Year Total payments ");
       printf("%d %.2f ", i+1,accPtr->array_w[i]);
       fprintf(in,"%d %.2f ", i+1,accPtr->array_w[i]);
   }
   fclose(in);
}

-----------------------------------------------------------------------------------------------

Enter Name: John
Social security: AAAAAAA
Enter principle P: 400000
Enter interest rate r: 5
Total years: 5
Year Total payments
1 87990.48
Year Total payments
2 175980.95
Year Total payments
3 263971.44
Year Total payments
4 351961.91
Year Total payments
5 439952.41

file annuity.data

Year       Total payments
1       87990.48
Year       Total payments
2       175980.95
Year       Total payments
3       263971.44
Year       Total payments
4       351961.91
Year       Total payments
5       439952.41