-
Notifications
You must be signed in to change notification settings - Fork 0
/
as2_invest.c
49 lines (43 loc) · 1.32 KB
/
as2_invest.c
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
46
47
48
49
// Ritvik Deshpande
#include <stdio.h>
int main(void)
{
float initial, rate, contribution, interest, balance;
int years;
// Obtain the required input values from the user, perform input validation
do
{
printf("Enter initial investment amount: ");
scanf("%f", &balance);
if (balance < 0) printf("Initial investment cannot be negative.\n");
} while (balance < 0);
do
{
printf("Enter total years: ");
scanf("%d", &years);
if (years <= 0) printf("Years must be greater than zero.\n");
} while (years <= 0);
do
{
printf("Enter return rate: ");
scanf("%f", &rate);
if (rate < 0) printf("Rate cannot be negative.\n");
} while (rate < 0);
do
{
printf("Enter additional contribution per year: ");
scanf("%f", &contribution);
if (contribution < 0) printf("Contribution cannot be negative.\n");
} while (contribution < 0);
// Create the investment returns table
printf("\n");
printf("Year Start Balance Interest End Balance\n");
printf("**********************************************************************\n");
for (int i = 1; i <= years; i++)
{
interest = balance * (rate / 100);
printf("%-10d%-20.2f%-20.2f%-20.2f\n", i, balance, interest, (interest + balance) );
balance += interest + contribution;
}
return 0;
}