//Jeremy Huang CS1A Chapter 3, P. 146, #16
//
/**************************************************************
*
* CALCULATE INTEREST EARNED
* ____________________________________________________________
* This program calculates the total amount of savings and
* interest earned after one year based on a given principal,
* interest rate, and the number of times the interest is
* compounded annually.
* ____________________________________________________________
* INPUT
* principal : intial balance in savings account
* interestRate : annual interest rate
* timesCompounded : times compounded per year
*
* OUTPUT
* interest : total interest earned in the year
* amountInSavings : final amount in savings account
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double principal; //INPUT - intial balance in savings account
double interestRate; //INPUT - annual interest rate
double interest; //OUTPUT - total interest earned in the year
double amountInSavings; //OUTPUT - total interest earned in the year
int timesCompounded; //INPUT - times compounded per year
//User Input
cout<<"Enter principal amount: ";
cin>>principal;
cout<<"\nEnter the annual interest rate (in percent): ";
cin>>interestRate;
cout<<"\nEnter the amount of times interest is compounded per year";
cin>>timesCompounded;
//Calculate final amount in savings
amountInSavings = principal * pow((1 + (interestRate/100.0)/
timesCompounded), timesCompounded);
//Calculate how much money earned in interest
interest = amountInSavings - principal;
//Output Result
cout<<"\n"<<fixed<<setprecision(2);
cout<<left<<setw(20)<<"Interest Rate:"<<right<<setw(10)<<setprecision(2)
<<interestRate<<"%"<<endl;
cout<<left<<setw(20)<<"Times Compounded:"<<right<<setw(11)<<timesCompounded
<<endl;
cout<<left<<setw(20)<<"Principal:"<<" $"<<right<<setw(9)<<principal<<endl;
cout<<left<<setw(20)<<"Interest:"<<" $"<<right<<setw(9)<<interest<<endl;
cout<<left<<setw(20)<<"Amount in Savings:"<<" $"<<right<<setw(9)
<<amountInSavings<<endl;
return 0;
}