Multiplication table

Write a program that displays a multiplication table.
Ask the user for the ending number then display table.
For example:

Enter a stopping number: 3

1*1=1
1*2=2
1*3=3

2*2=4
2*3=6

3*3=9

Notice that a particular pair of numbers is displayed only once; for example, the table includes
2*3=6 but does not include 3*2=6.

Use a pair of nested 'for' loops to write this program.



To any and all that would like to help! I have been flying through my programming class, but have no idea where to go with this problem. As of right now I'm completely stumped and would greatly appreciate some help!
Last edited on
Which bit, specifically, are you having trouble with?
This is nothing but a foor loop inside a for loop to construct this.
That's not a table. This is a table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// http://ideone.com/gAqulq
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>


void print_table(unsigned limit)
{
    std::cout << std::right << std::setw(5) << '|';
    for (unsigned i = 1; i <= limit; ++i)
        std::cout << std::setw(4) << i;
    std::cout << '\n' << std::string(5 + 4 * limit, '-') << '\n';

    for (unsigned i = 1; i <= limit; ++i)
    {
        std::cout << std::setw(4) << i << '|';
        for (unsigned j = 1; j <= limit; ++j)
            std::cout << std::setw(4) << i*j;
        std::cout << '\n';
    }
}

int main()
{
    std::cout << "Limit?\n> ";

    unsigned limit;
    std::cin >> limit;

    print_table(limit);
}


You should be able to modify the loops to produce something closer to what is required.
I'm completely lost on this one guys, I don't even know how to start and I can't figure out how to modify that code to help me. I need it to show what I mentioned above, how would I start it?
If you're truly lost, and can't even figure out how to start, here goes -

1. Write some code for generating the multiplication tables up to, say, 5*5 = 25.

I'm assuming you can do this, if you've been flying through your programming class. It's an example of a very basic "for" loop.

And cire may be correct about what you want not actually being a table, but let's agree to keep calling it that anyway, simply as a convenient verbal shorthand for the purposes of this thread.

Post the resulting code here. Make sure it's properly formatted and nicely commented. These are good habits to develop, right from the start.

2. Now, revise your code so that instead of displaying up to 5*5, it instead displays up to n*n, where n is a value chosen by the user.

This shouldn't be too difficult for you. It's a pretty basic example of:

1
2
3
std::cout << "How high should I go?: ";
int n;
std::cin >> n;


Post the resulting code to this thread.

3. Revise your code so as to check and make sure that the program doesn't display any particular pair of numbers twice. There are various ways to accomplish this.

4. Finally, tweak your code so that the output is displayed nicely and neatly, with blank lines where you want them and so forth.

This technique - of first writing code for a simplified version of the problem, and then incrementally revising that code so as to address the requirements which add complexity to the problem - is often quite useful. And it reflects how many real life, non-programming tasks are tackled.
Last edited on
Topic archived. No new replies allowed.