-
Interest program
Can someone please help me? Okay if a principal amount is P, for which the interest is compounded Q times per year, is placed in a savings account, then the amount of money in the account (the balance) after N years is given by the following formula where I is the annual interest rate as a floating-point number:
balance = P x (1 + I/Q) to the (N x Q) power. I have to write a program that inputs the values for P, I, Q, and N and outputs the balance for each year up through year N that uses a value-returning function to compute the balance and also prompts the user. Here is my code so far. Can someone tell me if I'm taking the right route or what else I need to do.
// Algorithm
// (1) Get input value for principal amount
// (2) Get annual interest rate
// (3) Get amount of times per year interest rate is compounded
// (4) Get amount of years interest is to be compunded
// (5) Use a value returning function to compute the balance
// (6) Output values
#include <iostream>
#include <iomanip>
float P, I, Q, N;
int main()
{
cout.setf(ios::fixed,ios::floatfield);
cout.setf(ios::showpoint);
cout<<setprecision(2);
cout<<"Enter principal amount"<<endl;
cin>>P;
cout<<"Enter annual interest amount"<<endl;
cin>>I;
cout<<"Enter amount of times interest is compunded per year"<<endl;
cin>>Q;
cout<<"Enter amount of years interest is to be compunded"<<endl;
cin>>N;
return P * (1 + (I / Q)^(N * Q);
}
-
Here is a tip, if you aren't absolutely sure it's in the langauge, don't use it. I had this problem before, and it made me really dislike programming. I had some experience with BASIC, and figured everything in BASIC should be in C++ as well. It will just add a lot of frustration. You need the pow in the <cmath> header to do this. In C++, this P * (1 + (I / Q)^(N * Q) looks like, also note that you were missing a closing paren.
P * ( 1 + pow(I/Q, N*Q));
-
Would this code work for what I wanted the program to do?
// Algorithm
// (1) Get input value for principal amount
// (2) Get annual interest rate
// (3) Get amount of times per year interest rate is compounded
// (4) Get amount of years interest is to be compounded
// (5) Use a value returning function to compute the balance
// (6) Output balance value
#include <iostream>
#include <iomanip>
#include <cmath>
float P, I, Q, N;
int main()
{
cout.setf(ios::fixed,ios::floatfield);
cout.setf(ios::showpoint);
cout<<setprecision(2);
cout<<"Enter principal amount"<<endl;
cin>>P;
cout<<"Enter annual interest amount"<<endl;
cin>>I;
cout<<"Enter amount of times interest is compunded per year"<<endl;
cin>>Q;
cout<<"Enter amount of years interest is to be compunded"<<endl;
cin>>N;
return P * (1 + pow(I/Q),N*Q));
}
-
I don't think you want to return the answer, return is there to tell the os if the program ran succesfully or not. Just use cout to show the answer instead.