-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinancialCalculator.cpp
More file actions
127 lines (97 loc) · 3.43 KB
/
FinancialCalculator.cpp
File metadata and controls
127 lines (97 loc) · 3.43 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Created by T Alpha 1 on 9/29/2019.
//
#include "FinancialCalculator.h"
void prompt() {
char input;
do {
std::cout << "(M)onthly loan payment, (I)nflation calculator,\n(R)eal interest rate, (Y)ears to savings goal\nEnter what you want to calculate: ";
std::cin >> input;
}
while (tolower(input) != 'm' && tolower(input) != 'i' && tolower(input) != 'r' && tolower(input) != 'y');
if (tolower(input) == 'm'){
monthlyLoan();
}
else if (tolower(input) == 'i'){
inflationCalculator();
}
else if (tolower(input) == 'r'){
realInterestRate();
}
else if (tolower(input) == 'y'){
yearToSavingsGoal();
}
}
void monthlyLoan() {
double principle;
double term;
double rates;
double payment;
std::cout << "Enter principal: ";
std::cin >> principle;
std::cout << "Enter term (years): ";
std::cin >> term;
std::cout << "Enter annual interest rate: ";
std::cin >> rates;
if (rates !=0) {
payment =(principle * (rates / 12) * pow((1 + (rates / 12)), (12 * term))) / (pow((1 + (rates / 12)), (12 * term)) - 1);
}
if (rates == 0){
payment = (principle/12)/(term);
}
std::cout << "Your monthly payment is $" <<std::fixed<< std::setprecision(2) << (payment) << "." << std::endl;
}
void inflationCalculator() {
double year;
double amount;
double endingYear;
double inflation;
double equivalence;
double time;
std::cout << "Enter the starting year: ";
std::cin >> year;
std::cout<<"Enter the amount of " << std::fixed << std::setprecision(0) << year << " dollars: ";
std::cin >> amount;
std::cout<<"Enter the ending year: ";
std::cin >> endingYear;
time = endingYear-year;
std::cout<<"Enter the inflation rate: ";
std::cin >> inflation;
equivalence = amount*pow((1 + inflation),time);
std::cout << "$" << std::fixed << std::setprecision(2) << amount << " in "<< std::fixed << std::setprecision(0) << year << " has the same value as " << "$" << std::fixed << std::setprecision(2) << equivalence << " in " << std::fixed << std::setprecision(0) << endingYear << "." << std::endl;
}
void realInterestRate() {
float nominalInterest;
float inflation;
float realInterest;
std::cout << "Enter nominal interest rate: ";
std::cin >> nominalInterest;
std::cout << "Enter inflation rate: ";
std::cin >> inflation;
realInterest = ((1+nominalInterest)/(1+inflation)) -1;
std::cout << "The real interest rate is " << std::fixed << std::setprecision(2) << realInterest << "." << std::endl;
}
void yearToSavingsGoal() {
float goal;
float current;
float contribution;
float interest;
float years;
std::cout << "Enter savings goal: ";
std::cin >> goal;
std::cout << "Enter current savings: ";
std::cin >> current;
std::cout << "Enter monthly contribution: ";
std:: cin >> contribution;
std::cout << "Enter annual interest rate: ";
std::cin >> interest;
if (interest !=0) {
//years = (log((goal + (contribution/(interest/12.0)))/(current + (contribution/(interest/12.0)))))/log(1.0+(interest/12.0));
years = log((goal+(contribution*12./interest))/(current + (contribution*12./interest)))/log(1+interest);
//years = log((goal + (contribution/(interest/12.)) + contribution)/(current + (contribution/(interest/12.)+ contribution)))/log(1 + (interest/12.));
}
if (interest == 0){
years = (goal - current)/(contribution) ;
}
std::cout << "The savings goal of $" << goal << " will be reached in " << years/12. <<" years." << std::endl;
}