# Help! (Monetary Calculations Using Only Integers)

Printable View

• 04-28-2002
Unregistered
Help! (Monetary Calculations Using Only Integers)
I am new to C++ programming, but I am trying to modify the following code so that I can calculate compound interest using only integers. I only clues I have are to (Treat all monetary amounts as integral numbers of pennies. Then "break" the result into its dollar portion and cents portion by using the division and modulus operations. Insert a period.) If anyone could help me along in the right direction or help me find resources that explain this I would greatly appreciate it. Thanks.

#include <iostream>

using std::cout;
using std::endl;
using std::ios;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>

int main()
{
double amount, // amount on deposit
principal = 1000.0, // starting principal
rate = .05; // interest rate

cout << "Year" << setw( 21 )
<< "Amount on deposit" << endl;

// set the floating-point number format
cout << setiosflags( ios::fixed | ios::showpoint )
<< setprecision( 2 );

for ( int year = 1; year <= 10; year++ ) {
amount = principal * pow( 1.0 + rate, year );
cout << setw( 4 ) << year << setw( 21 ) << amount << endl;
}

return 0;
}
• 04-29-2002
stumpert
I am not sure that i have completely understood your question(becorse of my english knowledge).

Just declear your variables as integer. To deal with floating numbers using integers you have to multiply with 10, 100, 1000 to move your decimal point to the left. Then when you are finished you do the opposite.

Remember that integers are always rounded down. So you have to do this manually.
• 04-29-2002
Unregistered
I'll try this and see what happens!
Thanxs
• 05-02-2002
Unregistered
Did not work! Someone please help!

Thanxs
• 05-02-2002
salvelinus
Try: 75 mod 10 = 5. 75 / 10 = 7 (using only integers). 862 mod 100 = 62, 862 / 100 = 8. And so on.
862 pennies, do the math above.
Code:

```int x = 862 % 100;  // x = 62 int y = 862 / 100;  // y = 8 cout << y << '.' << x;  // prints 8.62```
That should give you a start.
• 05-03-2002
Unregistered
I'll give it a shot!

Thanxs