Monetary / Compound Interest

VSBrown 0 Tallied Votes 217 Views Share

Uses only integers for monetary values to calculate compound interest.

/******************************************************

** Name: 

** Filename: monetary.cpp

** Project #: Deitel & Deitel 2.51

** Project Description: Modify the program in Fig 2.21 so it uses only
		   integers for monetary values to calculate the compound 
		   interest.

** Output: Table with year and amount of deposit as it grows  with 
           interest rate

** Input:  None

** Algorithm: Make all variables for money values as integers, then
              put into penny amount.
			  Run loop till year <= 10
			  Calculate yearly amount using formula a = p ( 1 + r )^n
			  Divide amount by 100 giving an integer result 
			  Divide amount using modulus to get remainder to add
			  cents amount 
			  Print amount followed by period then print cents
			  End program



******************************************************/

// Include files
#include <iostream>  // used for cin, cout
#include <conio.h>
#include <cmath>
#include <iomanip>
using namespace std;

// Global Type Declarations

// Function Prototypes
void instruct (void);
void pause ();

//Global Variables - should not be used without good reason.

int main ()
{
	 // Declaration section
	  int amount,            // amount on deposit
	      principal = 100000;// starting principal in pennies amount
   double rate = .05;        // interest rate 
		  
		 
	 // Executable section
	 instruct ();

   cout <<"Year" << setw ( 27 ) 
        << "Amount on deposit\n" << endl;
   
   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( 18 ) << amount / 100 
		    << '.';

	   if ( amount % 100 < 10 )
		   cout << '0' << amount % 100 << endl;
	   else 
		   cout << amount % 100 << endl;
   
   }
      
	 pause ();
	 return 0;
}

void instruct (void)
{
	  // Declaration section

	  // Executable section
}

void pause ()
{
    // Declaration section

    // Executable section
    cout << "\nPress any key to continue...";
    getch();
    cout << "\r";
    cout << "                            ";
    cout << "\r";
}


/*
Program Output

Year         Amount on deposit

   1              1050.00
   2              1102.50
   3              1157.62
   4              1215.50
   5              1276.28
   6              1340.09
   7              1407.10
   8              1477.45
   9              1551.32
  10              1628.89

Press any key to continue...



*/

Dani AI

Generated

Storing money as whole pennies is the right direction. It avoids the small but real errors you get when using binary floating point for financial values. is already partway there by switching to integer cents; the remaining risks come from using floating math (for example calling pow on a double rate) and from not being explicit about rounding and overflow.

A simple, robust pattern: keep the principal in cents using a 64-bit integer, express the interest rate as an integer scale (basis points or a fixed scale), and compound with integer arithmetic every period. This avoids casts from double and gives exact cents every year; add half the scale before dividing to round to nearest cent. Example (illustrative, not a rewrite of the posted program):

#include <cstdint>

int64_t cents = 1000 * 100;    // $1,000.00
const int SCALE = 10000;       // four-decimal scale
int rateScaled = 500;          // 5.00% => 500 (basis points in SCALE)

for (int year = 1; year <= 10; ++year) {
    cents = (cents * (SCALE + rateScaled) + SCALE/2) / SCALE; // rounded cents
    // print cents/100 and cents%100 with leading zero for cents
}

Practical tips: use int64_t to avoid overflow for realistic ranges; choose SCALE to support the smallest rate granularity you need (basis points, thousandths, etc.); if you compound monthly iterate 12 times per year with the monthly rate; be explicit about rounding rules (nearest-cent vs bankers rounding) to match accounting requirements; avoid nonportable headers like conio.h — use standard IO for portability. This keeps the computation deterministic and auditable, which is why integer money is standard in finance software.

Ene Uran 638 Posting Virtuoso

"Modify the program in Fig 2.21 so it uses only
integers for monetary values to calculate the compound
interest."
Why would you ever want to do that for?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.