-2

I want to make a 12X12 multiplication table:

#include <iostream>
using namespace std;

int main(){

    for (int j = 1; j < 13; j++)
        for (int i =1; i < 13; i++)
            cout << j * i << " ";
    cout << endl;
    return 0;
}

1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144

This is what I get, but I want it to look better, like:

1.  1 2 3 4 5 6
2.
3.
4.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user12230466
  • 27
  • 1
  • 1
  • 8

3 Answers3

0

There are one million answers and the question is not so precise. So, I need to make some assumptions regarding alignment and output format.

Addtionally I will use modern C++ features to be more flexible.

First we define a Lampda. In that we will use an ostringstream for easier output using the streams library. Then we simply loop over the requested number of elements and create one row. Additionally, we do some formating stuff.

At the end we use an algorithm from the STL to create the full table.

#include <iostream>
#include <algorithm>
#include <iterator>
#include <iomanip>

constexpr int Max{12};

int main()
{
    // Define generator function Lambda
    auto generator = [row = 1]() mutable -> std::string  {
        // We will store a temporary result for one line in an ostringstream
        std::ostringstream oss{};

        // Output and formatting
        oss << std::right << std::setw(2) << row << ". "; 
        for (int i=1; i<=Max; ++i)
            oss << std::setw(3) << i*row << " ";
        oss << "\n";
        ++row;
        // The resulting line
        return oss.str();
    };

    // Generate the table
    std::generate_n(std::ostream_iterator<std::string>(std::cout, " "), Max,generator);
}

A M
  • 14,694
  • 5
  • 19
  • 44
-1
#include <iostream>
using namespace std;

int main()
{
    for (int j = 1; j < 13; j++)
    {
        cout << j << ".";
        for (int i=1; i < 13; i++)
        {
            cout << i *j << " ";
        }
        cout << endl;
    }
    return 0;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1
#include <iostream>
using namespace std;
int main() {

    for (int j = 1; j < 13; j++)
    {
        cout << j << "x12 TABLE:\t";
        for (int i = 1; i < 13; i++)
        {
            cout << i *j << "\t";
        }
        cout << endl;
    }
    return 0;
}

//OUTPUT:
//1x12 TABLE : 1       2       3       4       5       6       7       8       9       10      11      12
//2x12 TABLE : 2       4       6       8       10      12      14      16      18      20      22      24
//3x12 TABLE : 3       6       9       12      15      18      21      24      27      30      33      36
//4x12 TABLE : 4       8       12      16      20      24      28      32      36      40      44      48
//5x12 TABLE : 5       10      15      20      25      30      35      40      45      50      55      60
//6x12 TABLE : 6       12      18      24      30      36      42      48      54      60      66      72
//7x12 TABLE : 7       14      21      28      35      42      49      56      63      70      77      84
//8x12 TABLE : 8       16      24      32      40      48      56      64      72      80      88      96
//9x12 TABLE : 9       18      27      36      45      54      63      72      81      90      99      108
//10x12 TABLE : 10      20      30      40      50      60      70      80      90      100     110     120
//11x12 TABLE : 11      22      33      44      55      66      77      88      99      110     121     132
//12x12 TABLE : 12      24      36      48      60      72      84      96      108     120     132     144

luck
  • 165
  • 1
  • 10
  • The output formatted with '\t' looks OK, but my suggestion is to use a pre-defined width for each number. See [this topic](https://stackoverflow.com/a/11226376/9233849). – Daniel Shao Nov 01 '19 at 05:15