//Renee V. Lopez CS1A Chapter 3, Pg.146, #16
/*******************************************************************************
* CALCULATE COMPOUND INTEREST
* _____________________________________________________________________________
* This program calculates the total amount in a savings account and the interest
* earned after compounding based on user input for the principal, interest rate,
* and number of times the interest is compounded per year.
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main () {
// Declare variables to hold user inputs and results
float interestRate; // Annual interest rate as a percentage
float timesCompounded; // Number of times interest is compounded per year
float principal; // Initial amount in the savings account
float interest; // Used for storing intermediate interest calculation
float amountinSavings; // Final amount after adding interest
float interestEarned; // Total interest earned
// Gather input from user
cout << "Enter the Interest Rate (in percentage): " << endl;
cin >> interestRate;
cout << "Enter the number of times interest is compounded per year: " << endl;
cin >> timesCompounded;
cout << "Enter the initial principal amount: " << endl;
cin >> principal;
// Perform calculations
interestRate /= 100; // Convert interest rate to decimal
interest = 1 + (interestRate / timesCompounded); // Calculate interest factor
amountinSavings = principal * pow(interest, timesCompounded); // Final amount in savings
interestEarned = amountinSavings - principal; // Calculate interest earned
// Display results with appropriate formatting
cout << fixed << setprecision(2);
cout << "Interest Earned: $" << interestEarned << endl;
cout << "Total Amount in Savings: $" << amountinSavings << endl;
return 0;
}