In: Computer Science
Write a program that calculates the compound interest for an investment. (C++ coding language)
If you deposit an amount of money P , the principal, at an interest rate r then the interest will compound over time. This means that the interest earned each period becomes part of the principal and the next time you get interest you earn interest on the interest. This is known as compounding.
The equation for compound interest is
( r)n·t Pn=P0 1+n
where P0 is the starting amount in-
vested, Pn is the ending amount, r is
the annual interest rate as a
decimal, and n is the number of compounding periods per year, and t
is the number of years.
The user will enter the starting principal, the annual interest rate as a percentage, the number of compounding periods per year, and the number of years.
The program calculates the interest earned and the final principal.
The two principals, the interest, and the interest rate must be doubles. The number of compounding periods and the number of years must be integers.
Your program must also include comments with your name, the date, and a short description of the project. It must also print this information as a splash screen at the beginning of the program run.
Princ. Start | $1000.00 |
Interest Rate | 6% |
Periods |
12 |
Years | 10 |
Interest | $819.40 |
Princ. End | $1819.40 |
Use this as a template:
// Pound includes - will need the string and cmath libraries
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main(void) {
// Splash Screen
// Declare and Initialize all of the variables
// Will need doubles for starting and ending principal, and annual interest
// rate, and interest earned
// Integers for number of compounding periods per year, and number of years
int periods_per_year;
double start_principle;
// Prompt for entering data
// Calculate the final amount
// Calculate the interest earned
// Print the results
return 0;
}
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the
answer. Thanks!
===========================================================================
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main(void) {
// Splash Screen
cout<<"===============================================================\n";
cout<<"Program calculate the Compund Interest of
an Initial Investment\n";
cout<<"===============================================================\n\n";
// Declare and Initialize all of the variables
// Will need doubles for starting and ending
principal, and annual interest
// rate, and interest earned
double principal, ending_principal, annual_interest,
interest_earned;
// Integers for number of compounding periods per
year, and number of years
int periods, years;
// Prompt for entering data
cout<<"Enter the initial principal amount: ";
cin >> principal;
cout<<"Enter the annual interest rate as
percentage: "; cin >> annual_interest;
cout<<"Enter the number of years: "; cin
>> years;
cout<<"Enter period of compounding per year: ";
cin >> periods;
// Calculate the final amount
ending_principal =
principal*pow((1+annual_interest*0.01/periods),periods*years);
// Calculate the interest earned
interest_earned = ending_principal-principal;
// Print the results
cout<<setprecision(2)<<fixed<<showpoint;
cout<<"\nInterest Earned:
$"<<interest_earned<<endl;
cout<<"Ending Principal:
$"<<ending_principal<<endl;
return 0;
}
=================================================================