65,210
社区成员
发帖
与我相关
我的任务
分享#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 = 1000.0; // initial amount before interest
double rate = .05; // interest rate
// 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 );
// display the year and the amount
cout << setw( 4 ) << year << setw( 21 ) << amount << endl;
} // end for
return 0; // indicate successful termination
} // end main
我的通过了
d:\>g++ test.cpp
test.cpp:35:15: warning: no newline at end of file
d:\>a
Year Amount on deposit
1 1050.00
2 1102.50
3 1157.63
4 1215.51
5 1276.28
6 1340.10
7 1407.10
8 1477.46
9 1551.33
10 1628.89