Interest Earned
This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.
I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.
3.16: Assuming there are no deposits other than the original investment, the balance in a savings account after one year may be calculated as:
Amount = Principal * (1 + (Rate/T))^T
Principal is the balance in the savings account, Rate is the interest rate, and T is the number of times the interest is compounded during a year (T is 4 if the interest is compounded quarterly).Write a program that asks for the principal, the interest rate, and the number of times the interest is compounded. It should display a report similar to:
Interest Rate: 4.25%
Times Compounded: 12
Principal: $ 1000.00
Interest: $ 43.34
Amount in Savings: $ 1043.34
My solution:
/* Chapter 3 Challenge 16
Written by Kirk Hingsberger
September 26, 2007
Interest Earned
*/
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float principal, rate, currentBalance, interestAmount;
int t; // t is number of times interest compounded annually
cout << "Please enter the principal amount: ";
cin >> principal;
cout << "\nPlease enter the interest rate: ";
cin >> rate;
cout << "\nPlease enter the number of times interest is compounded annually: ";
cin >> t;
currentBalance = principal * pow((1 + rate / t), t);
interestAmount = currentBalance - principal;
cout << left << setw(20) << "Interest Rate:" << right << setw(9);
cout << setprecision(2) << fixed << rate << "%" << endl;
cout << left << setw(20) << "Times Compounded:" << right << setw(9) << t << endl;
cout << left << setw(20) << "Principal:" << "$" << right << setw(8) << principal << endl;
cout << left << setw(20) << "Interest:" << "$" << right << setw(8) << interestAmount << endl;
cout << left << setw(20) << "Amount in Savings:" << "$" << right << setw(8) << currentBalance << endl;
return 0;
}
Here is the capture script I turned in for grading:
Script started on Thu 27 Sep 2007 01:25:46 PM MDT
$ cat chap3chlg16.cc
/* Chapter 3 Challenge 16
Written by Kirk Hingsberger
September 26, 2007
Interest Earned
*/
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float principal, rate, currentBalance, interestAmount;
int t; // t is number of times interest compounded annually
cout << "Please enter the principal amount: ";
cin >> principal;
cout << "\nPlease enter the interest rate: ";
cin >> rate;
cout << "\nPlease enter the number of times interest is compounded annually: ";
cin >> t;
currentBalance = principal * pow((1 + rate / t), t);
interestAmount = currentBalance - principal;
cout << left << setw(20) << "Interest Rate:" << right << setw(9);
cout << setprecision(2) << fixed << rate << "%" << endl;
cout << left << setw(20) << "Times Compounded:" << right << setw(9) << t << endl;
cout << left << setw(20) << "Principal:" << "$" << right << setw(8) << principal << endl;
cout << left << setw(20) << "Interest:" << "$" << right << setw(8) << interestAmount << endl;
cout << left << setw(20) << "Amount in Savings:" << "$" << right << setw(8) << currentBalance << endl;
return 0;
}
$ g++ -o chap3chlg16 chap3chlg16.cc -s
$ chap3chlg16
Please enter the principal amount: 10000
Please enter the interest rate: .0672
Please enter the number of times interest is compounded annually: 6
Interest Rate: 0.07%
Times Compounded: 6
Principal: $ 10000.00
Interest: $ 691.09
Amount in Savings: $ 10691.09
$ exit
exit
Script done on Thu 27 Sep 2007 01:26:57 PM MDT










