SOURCE FILE: 090_intrstLoop.cpp
/* Program to calculate compound annual interest.
Note the indentation style - it is important that
all students use a similar style.
*/
#include <iostream>
using namespace std ;
int main (void)
{
int Y_years, years_left ;
double R_percent, R_decimal_rate ;
double amount = 1 ;
cout << "\nI can tell you how much a dollar will be worth \n"
<< "in Y years at interest rate R\n\n"
<< "Please enter the number of years Y"
<< " as a whole positive number: " ;
cin >> Y_years ;
cout << "\nPlease enter the annual interest rate R\n"
<< " as a percentage (e.g. 6.25): " ;
cin >> R_percent ;
R_decimal_rate = R_percent/100 ;
years_left = Y_years ;
while (years_left > 0)
{
amount = amount * (1+R_decimal_rate) ;
years_left = years_left - 1 ;
cout << "years_left is: " << years_left
<< " and amount is: " << amount << endl;
// another way: years_left-- ; (decrement operator)
}
cout << "\nAfter " << Y_years << " year(s) at interest rate "
<< R_percent << " percent" << endl ;
cout << "You will have " << amount << endl ;
cout << endl ;
return 0;
}