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: 3580567 • 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 Wi in the ith year are calculated by the formulas,

and

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 Wi 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 to the structure.

Call function Annuity with structure as argument

Print the data in the following formats: Name:

Total years: Interest Rate: Principal :

Annual withdrawal

Year               Total payments

1

2

3

4

5

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 Wi and store it in the array of the structure.

[Hint: you may want to use pointer to structure and pass the data by reference.]

(1 n-1 r P( (1 r) 1

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;
   int 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;
   int r, n;
     
   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("%d", &r);
   account.r = r;

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

void Annuity(Account *accPtr)
{
   int i;

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

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 ", accPtr->array_w[i]);
   }

}