//***********************************************************************
// This is a sample C++ program which accepts an integer as input
// and then echoes that integer along with its square and its cube.
// It also reports the average of the cube, the square, and the original
// integer.
// January 6, 2005
// W. R. Nico
//************************************************************************
// Include any necessary header files. "iostream" gives
// access to basic stream input and output.
#include
// The "using namespace" command makes short names available,
// without the need for a "scope resolution" qualifier, e.g.
// allowing for use of "cout" rather than "std::cout".
using namespace std;
// "main" is a function that returns an int. It is written
// either taking no arguments, i.e., as "int main()", or
// enabled to take "command line" arguments, i.e.,
// "int main(int argc, char * argv[])".
int main(){ //Style note: Some prefer the opening brace on the next line.
int x, x_sq, x_cub; // Declare three integer variables.
float average; // The average desired will usually not be an integer.
// Prompt for some input. We'll not put a newline at the
// end so that the user can enter the integer on the same line.
cout << "Enter an integer: ";
// Accept an integer from the input.
cin >> x;
x_sq = x*x; // Compute the square.
x_cub = x_sq*x; // and now the cube.
// Now compute the average. Note the use of "3.0" is important!
average = (x + x_sq + x_cub)/3.0;
// Now write out the desired results. Note that the spaces
// inside the quoted strings are needed for readable output.
// Note also that "endl" is what generates end-of-line on the
// actual output, not the way things are laid out here.
// This layout is for readability of the program itself.
cout << "The square of " << x << " is " << x_sq << ", and ";
cout << "the cube is " << x_cub << "." << endl;
cout << "The average of these three values is " << average << endl;
// "main" needs to return an int (one that's < 256). Traditionally, when
// everything is OK, 0 is the value to return.
return 0;
} // This closing brace matches the opening one and ends the program.
// This is a simple program but contains a possible security flaw!