Skip to content

Instantly share code, notes, and snippets.

@bichanna
Created April 11, 2023 23:26
Show Gist options
  • Save bichanna/2f99fd6e09abd4dc45827dff590c2253 to your computer and use it in GitHub Desktop.
Save bichanna/2f99fd6e09abd4dc45827dff590c2253 to your computer and use it in GitHub Desktop.
#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