Uses only integers for monetary values to calculate compound interest.
Monetary / 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...
*/
Ene Uran 638 Posting Virtuoso
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.