Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- // You have an investment that doubles
- // at the end of every 5 years.
- // Write a program that takes amount and years and
- // retuns the value of the investment.
- const int yrs_to_double = 5; // Doubles at end of 5 years.
- // Calculate the final value, given amount and years.
- float growth(float amount, float years)
- {
- // How many 5-year periods are there?
- int number_of_doublings = years / yrs_to_double;
- float current_doubling = 0; // Each doubling.
- for (int i = 0; i < number_of_doublings; ++i)
- {
- current_doubling = amount * 2; // Double it.
- amount = current_doubling; // Store the doubling.
- }
- return amount; // Return the final amount.
- }
- int main()
- {
- float amount_invested = 0; // Amount to start with.
- int years_to_invest = 0; // Years invested.
- float value_of_investment = 0; // Value after investing.
- cout << "Enter the amount you are investing: ";
- cin >> amount_invested; // Get the starting amount.
- cout << "How many years?: ";
- cin >> years_to_invest; // Get the time to invest.
- // Calculate the value.
- value_of_investment = growth(amount_invested, years_to_invest);
- // Show two decimal points.
- cout << "After investing $" << fixed << setprecision(2)
- << amount_invested << " for " << years_to_invest
- << " years,\nyou now have $" << value_of_investment << ".\n";
- return 0; // End the program.
- }
Add Comment
Please, Sign In to add comment