#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int years, timesCompounded; //The number of years deposited, the number of times per year compounded
double interestRate, principal; //The nominal annual interest rate, the amount of money deposited.
years = 10;
timesCompounded = 4;
interestRate=5;
principal = 100;
cout<<"If you deposit "<<principal<<"at "<<interestRate<<" nominal annual interest rate ";
cout<<"compounded "<<timesCompounded<<" times per year for " <<years<<" years,\n";
cout<<"you will have "<<principal*pow(1+interestRate/(100*timesCompounded), timesCompounded*years)<<" in your account.\n";
years = 20;
timesCompounded = 12;
interestRate=5;
principal = 100;
cout<<"If you deposit "<<principal<<" at "<<interestRate<<" nominal annual interest rate ";
cout<<"compounded "<<timesCompounded<<" times per year for " <<years<<" years,\n";
cout<<"you will have "<<principal*pow(1+interestRate/(100*timesCompounded), timesCompounded*years)<<" in your account.\n";
years = 50;
timesCompounded = 365;
interestRate=5;
principal = 100;
cout<<"If you deposit "<<principal<<" at "<<interestRate<<" nominal annual interest rate ";
cout<<"compounded "<<timesCompounded<<" times per year for " <<years<<" years,\n";
cout<<"you will have "<<principal*pow(1+interestRate/(100*timesCompounded), timesCompounded*years)<<" in your account.\n";
system ("pause");
return 0;
}
My professor gave us this code. I understand how functions work but the assignment was to make a function so you wouldn't need to the 3 sets of couts.
cout<<"If you deposit "<<principal<<" at "<<interestRate<<" nominal annual interest rate ";
cout<<"compounded "<<timesCompounded<<" times per year for " <<years<<" years,\n";
cout<<"you will have "<<principal*pow(1+interestRate/(100*timesCompounded), timesCompounded*years)<<" in your account.\n";
is repeated each time?
If you made a function that did this, you could call that function where in the code you currently have those lines.
I understand what the function will do. I just can't make a function that prints cout's. If i had to make the function do some kind of math no problem but I can't seem to get one to print couts