# My program works but the numbers are not correct.

• 07-22-2003
romeoz
My program works but the numbers are not correct.
here is my assisgnment and I can compile it and run it but the numbers dont seem to be correct....could someone take a look and see if my call function is wrong? thanks

Bryan

Assignment:
If a principal amout P, for which the interest is compunded 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)^NxQ

Write a C++ program that inputs the values for P, I, Q, and N and outputs the balance for each year up through year N. Use a value-returning function to compute the balance. Your program should prompt the user appropriately, label the output values, and have a good style.

Code:

```#include <iostream> #include <cmath> void Balance(float, float, int, int, float&); using namespace std; int main() {         float P,I,B;         int Q,i,N;         cout << "Enter the principal: ";         cin >> P;         cout << "Enter the annual interest rate: ";         cin >> I;         cout << "Enter the number of times money compounded per year: ";         cin >> Q;         cout << "Enter the number of years: ";         cin >> N;         for (i = 1; i <= N; i++)         {                 Balance(P,I,Q,i,B);                 cout << "The balance after " << i << " year(s), is: "                         << B << "." << endl;         }         return 0; }         void Balance(float P, float I, int Q, int N, float& B) {         B = (P * pow((1.0 + I/Q),N*Q)); }```
• 07-22-2003
Zach L.
Function call looks correct. You may be getting truncation errors due to the use of the integers, however.

Try changing 1.0+I/Q to 1.0+I/static_cast<float>(Q).
• 07-22-2003
romeoz
that change gives me the same results...this is my results...I dont think this is correct.

Enter the principla: 15000
Enter the annual interest rate: 5.9
Enter the number of times money componded per year: 6
Enter the number of years: 7

The balance after 1 year(s), is: 912989.
The balance after 2 year(s), is: 5.55699e+007
The balance after 3 year(s), is: 3.38232e+009
The balance after 4 year(s), is: 2.05868e_011
The balance after 5 year(s), is: 1.25303e_013
The balance after 6 year(s), is: 7.62671e+014
The balance after 7 year(s), is: 4.64207e_016
Press any key to continue
• 07-22-2003
romeoz
I needed to enter the persentage as .059 vs 5.9. I did that and it works great. Here is the final code in case any one wants to see the difference. Also I made it a return statement vs void.

Bryan
Code:

```#include <iostream> #include <cmath> float Balance(float, float, int, int, float&); using namespace std; int main() {         float P,I,B;         int Q,i,N;         cout << "Enter the principal: ";         cin >> P;         cout << "Enter the annual interest rate: ";         cin >> I;         I=I/100;         cout << "Enter the number of times money compounded per year: ";         cin >> Q;         cout << "Enter the number of years: ";         cin >> N;         for (i = 1; i <= N; i++)         {                 Balance(P,I,Q,i,B);                 cout << "The balance after " << i << " year(s), is: "                         << B << "." << endl;         }         return 0; }         float Balance(float P, float I, int Q, int N, float& B) {         return B =(P * pow((1.0 + I/Q),N*Q)); }```
• 07-22-2003
Zach L.
Great. :)
Now if you'd only sell the original version to my bank. ;)