/* filename: powers.cpp Program to demonstrate: nested "for" loops Purpose: To display powers 1 through 5 of numbers 1 through 5 in the output window Reference: Savitch, Chapter 7.4 *********************************************************************/ #include #include #include using namespace std; int main() { const int numPowers = 5; // Define a constant to store the number // of loop iterations int base = 0, power = 0; // Declare and define variables char anyKey = 0; // Print headings cout << setw(4) << "Base" << setw(10) << "Power" << setw(15) << "Base^Power" << endl; cout << setw(4) << "----" << setw(10) << "-----" << setw(15) << "----------" << endl; // The code below displays powers 1 through 5 of numbers 1 through 5. // The outer loop determines the base, while the inner loop determines // the power. for (base = 1; base <= numPowers; base++) { for (power = 1; power <= numPowers; power++) cout << setw(4) << base << setw(10) << power << setw(15) << pow(base, power) << endl; // Print a blank line between different bases cout << endl; // Pause display so output can be read if (base == 2) { cout << "\nPress any key, then , to continue ..."; cin >> anyKey; cout << endl; } // end if } // end outer for cout << endl; return 0; }