Created
April 11, 2023 23:26
-
-
Save bichanna/2f99fd6e09abd4dc45827dff590c2253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
using namespace std; | |
int years; | |
double initial_amount, amount_to_add, rate; | |
double calculate(double prev, int count, int years) { | |
return (years == count) | |
? prev | |
: calculate(prev * rate + amount_to_add, ++count, years); | |
} | |
int main() { | |
cout << "Enter the rate: "; | |
cin >> rate; | |
cout << "Enter how many years to invest: "; | |
cin >> years; | |
cout << "Enter the initial amount you start with: "; | |
cin >> initial_amount; | |
cout << "Enter the amount you add each year: "; | |
cin >> amount_to_add; | |
double result{calculate(initial_amount, 1, years)}; | |
cout << fixed << setprecision(2) << "Total amount: $" << result << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment