topical media & game development

talk show tell print

basic-program-code-03-Ex3-14.c

? / basic-program-code-03-Ex3-14.c


  // Ex3_14.cpp
  // Using nested loops to generate a multiplication table
  include <iostream>
  include <iomanip>
  
  using std::cout;
  using std::endl;
  using std::setw;
  
  int main()
  {
     const int size = 12;              // Size of table
     int i = 0, j = 0;                 // Loop counters
  
     cout << endl                        // Output table title
          << size << " by " << size
          << " Multiplication Table" << endl << endl;
  
     cout << endl << "    |";
     for(i = 1; i <= size; i++)          // Loop to output column headings
        cout << setw(3) << i << "  ";      
  
     cout << endl;                       // Newline for underlines
     for(i = 0; i <= size; i++)
        cout << "_____";                 // Underline each heading
  
     for(i = 1; i <= size; i++)          // Outer loop for rows
     {
        cout << endl
             << setw(3) << i << " |";    // Output row label
  
        for(j = 1; j <= size; j++)           // Inner loop for the rest of the row
           cout << setw(3) << i*j << "  ";   // End of inner loop
  
     }                                   // End of outer loop
     cout << endl;
     return 0;
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.