Print Friendly and PDF
(Compound Interest Calculation) Modify Fig. 5.6 so it uses only integers to calculate the compound interest. [Hint: Treat all monetary amounts as numbers of pennies. Then “break” the result into its dollar and cents portions by using the division and modulus operations. Insert a period.]
(Compound Interest Calculation) Modify  so it uses only integers to calculate the compound interest. 

[Hint: Treat all monetary amounts as numbers of pennies. Then “break” the result into its dollar and cents portions by using the division and modulus operations. Insert a period.]
Solution: 
 

#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
 
#include <iomanip>
using std::setw; // enables program to set a field width
using std::setprecision;
 
#include <cmath> // standard C++ math library
using std::pow; // enables program to use function pow
 
int main()
{
   int amount; // amount on deposit at end of each year
   int principal = 1000; // initial amount before interest
   double rate = .05; // interest rate
   int dollar;
   int cent;
 
   // display headers
   cout << "Year" << setw( 21 ) << "Amount on deposit" << endl;
 
   // set floating-point number format
   cout << fixed << setprecision( 2 );
 
   // calculate amount on deposit for each of ten years
   for ( int year = 1; year <= 10; year++ ) 
   {
      // calculate new amount for specified year
      amount = (principal * pow( 1.0 + rate, year )) * 100;
 
      // dollars and cents
      dollar = amount/100;
      cent = amount%100;      
 
      // display the year and the amount
      cout << setw( 4 ) << year << setw( 21 ) << dollar << "." << cent << endl;
   } // end for 
 
   system("pause");
   return 0; // indicate successful termination
} // end main
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: