Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Write a program that prompts an  integer, and write multiplication table from 1 to that number. For example if the integer is 3 the output should be
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 16 18 20
3 6 9 12 15 18 21 24 27 30

Use nested for loops (two for loops one outer loop, one inner loop).


What I have tried:

C++
#include <iostream>

using namespace std;

int main()
{
    int num ,b,c,d;

  cout <<"Enter an  number to get the  multiplication table from 1 to that number"<<endl;
cin>>num;
for (b=1;b<=num;b++)
{ for(c=1;c<=num;c++)
{ d=b*c;
cout<<d;}
cout<<endl;}

  return 0;
}
Posted
Updated 4-Dec-16 9:25am
Comments
CHill60 4-Dec-16 14:57pm    
What is the problem?

Use the debugger, by allowing you to see your code executing, it will help you to understand what is wrong.
In the example, each line stops at 10, 1 of your loops must stops at 10 too.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
In contrast to your sample, where you are run from 1 to 10 on one axis and from 1 to n on the other, you run from 1 to n on both axis in your code...

Change the inner loop to run up to 10, and for the readability add a tab after each number printed...

C++
#include <iostream>

using namespace std;
 
int main()
{
    int num, b, c, d;
 
    cout << "Enter an  number to get the  multiplication table from 1 to that number" << endl;
    cin >> num;
    
    for (b = 1; b <= num; b++)
    { 
        for(c = 1; c <= 10; c++)
        {
            d = b * c;

            cout << d << "\t";
        }
        cout << endl;
    }

    return 0;
}</iostream>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900