Print Friendly and PDF
(Compound Interest) Modify the compound interest program of Section 5.4 to repeat its steps for the interest rates 5%, 6%, 7%, 8%, 9% and 1 0%. Use a for statement to vary the interest rate. Solution:
(Compound Interest) Modify the compound interest program of Section 5.4 to repeat its steps for the interest rates 5%, 6%, 7%, 8%, 9% and 1 0%. Use a for statement to vary the interest rate.

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()
{
double amount; // amount on deposit at end of each year
double principal;
// display headers
cout << setw( 4 ) << "Year" << setw( 9 ) << "Dep. (%X) " <<endl;
// set floating‐point number format
cout << fixed << setprecision( 2 ) ;
for( int rate = 5; rate <= 10; rate++) {

principal = 1000. 0;
// calculate amount on deposit for each of ten years
for ( int year = 1; year <= 10; year++)
{
principal = principal * pow( 1 + rate/100. 0, year ) ;
cout << setw( 4 ) << year << setw( 9 ) << principal << endl;
}
cout << 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: