-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw5a.2.c
63 lines (56 loc) · 1.5 KB
/
w5a.2.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Title: Calculate investment
* File: w5a.2.c
* Author: Nguyen Minh Quan
* Date: 20 Jan 2022
*/
#include <stdio.h>
#include <math.h>
/*
* Function: find_future
* ---------------------
* Find the future value of the investment after a period.
*
* params principal: initial value
* rate: annual rate
* period: investment period, measured by year
*
* returns: the future value of the investment
*/
double find_future(double principal, double rate, double period) {
return pow(1 + rate, period) * principal;
}
/*
* Function: find_principle
* ------------------------
* Find the principal value before investing
*
* params future: value after investment
* rate: annual rate
* period: investment period, measured by year
*
* returns: the principal value
*/
double find_principle(double future, double rate, double period) {
return future / pow(1 + rate, period);
}
int main() {
double v, r, p;
printf("1. Test saving account\n");
printf("principal = ");
scanf("%lf", &v);
printf("Annual rate = ");
scanf("%lf", &r);
printf("No of years = ");
scanf("%lf", &p);
printf("principal = %.2lf\n", v);
printf("amount after 2 years = %.2lf\n", find_future(v, r, p));
printf("2. Investment calculation\n");
printf("future = ");
scanf("%lf", &v);
printf("Annual rate = ");
scanf("%lf", &r);
printf("No of years = ");
scanf("%lf", &p);
printf("principal should be = %.2lf", find_principle(v, r, p));
}