summaryrefslogtreecommitdiff
path: root/07_retirement/retirement.c
blob: fd2d5c5fa4f8c554f31bf69fabdf659ed5a472be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>

struct _retire_info {
  int months;
  double contribution;
  double rate_of_return;
};
typedef struct _retire_info retire_info;

void retirement (int startAge,   //in months
                 double initial, //initial savings in dollars
                 retire_info working, //info about working
                 retire_info retired) //info about being retired
{
  double balance = initial;
  int age = startAge;
  for (int i = 1; i <= working.months; i++) {
    printf("Age %3d month %2d you have $%.2lf\n", age / 12, age % 12, balance);
    balance = balance * (1 + working.rate_of_return) + working.contribution;
    age++;
  }

  for (int i = 1; i <= retired.months; i++) {
    printf("Age %3d month %2d you have $%.2lf\n", age / 12, age % 12, balance);
    balance = balance * (1 + retired.rate_of_return) + retired.contribution;
    age++;
  }

}

int main (void) {
  retire_info working;
  working.months = 489;
  working.contribution = 1000;
  working.rate_of_return = 0.045/12;

  retire_info retired;
  retired.months = 384;
  retired.contribution = -4000;
  retired.rate_of_return = 0.01/12;

  retirement(327, 21345, working, retired);
  return EXIT_SUCCESS;
}