Multiplication Table - nested for loop

In summary, this code uses nested loops to print a multiplication table of size M by N. It prompts the user to enter the width and length of the table and then uses a nested for loop to print the table. The top header is aligned with the rest of the table using setw() and the code ends with a system pause.
  • #1
Marioqwe
68
4
Multiplication Table -- nested for loop

Homework Statement



Use nested loops to print a M by N multiplication table as following. Allow the user to enter the size of the table (M by N). 1 2 3 4 5 6 ... M
1 1 2 3 4 5 6 ... M
2 2 4 6 8 10 ... 2M
...
N N 2N ....... NM

Homework Equations


The Attempt at a Solution



Code:
#include<iostream>
#include<iomanip>

using namespace std;

int main()

{
	int w, l;
	cout << "Please enter width 'w'" <<endl;
	cin >> w;
	cout << "Please enter length 'l'" << endl;
	cin >> l;
	
	// TOP HEADER

        cout << "      ";
	int k;
	for( k=1; k<=w; k++)
	{
		cout << setw(4) << k <<  " |";
	}
	cout << endl;

	// NEST LOOP FOR MULT TABLE

	int i, j, result;
	for( i=1; i<=l; i++ )
	{
		cout << setw(4) << i << " :";
		for( j=1; j<=w; j++ )
			{
				result = i*j;
				cout << setw(4) << result <<  " |";
			}
		cout << endl;
	}			
	system("pause");
	return (0);
}

I don't know how to get the header to move to the right so that it is aligned with the other columns. Could anybody give me any hints?

Thank You!NEVERMIND.

My head enlightened and I figured it out :)
 
Physics news on Phys.org
  • #2
#include<iostream>#include<iomanip>using namespace std;int main(){ int w, l; cout << "Please enter width 'w'" <<endl; cin >> w; cout << "Please enter length 'l'" << endl; cin >> l; // TOP HEADER cout << setw(4) << " " << "|"; int k; for( k=1; k<=w; k++) { cout << setw(4) << k << " |"; } cout << endl; // NEST LOOP FOR MULT TABLE int i, j, result; for( i=1; i<=l; i++ ) { cout << setw(4) << i << " :"; for( j=1; j<=w; j++ ) { result = i*j; cout << setw(4) << result << " |"; } cout << endl; } system("pause"); return (0);}
 

1. What is a "Multiplication Table - nested for loop"?

A Multiplication Table - nested for loop is a mathematical tool used to display the multiplication table in a structured format. It is created using nested for loops, where the outer loop controls the rows and the inner loop controls the columns.

2. How does a "Multiplication Table - nested for loop" work?

The "Multiplication Table - nested for loop" works by using nested for loops to iterate through the numbers in the multiplication table and display them in a structured format. The outer loop controls the rows and the inner loop controls the columns, allowing for all the numbers to be displayed in a table.

3. What are the benefits of using a "Multiplication Table - nested for loop"?

There are several benefits of using a "Multiplication Table - nested for loop". It allows for the multiplication table to be displayed in a structured and organized format, making it easier to read and understand. It also helps in practicing and memorizing multiplication facts, as well as understanding the concept of nested loops in programming.

4. How can I create a "Multiplication Table - nested for loop"?

To create a "Multiplication Table - nested for loop", you will need to use a programming language that supports nested for loops, such as Python, Java, or C++. You will need to create an outer loop that controls the rows and an inner loop that controls the columns. Within the inner loop, you will need to perform the multiplication operation and display the result.

5. Can I customize the "Multiplication Table - nested for loop" to display specific numbers?

Yes, you can customize the "Multiplication Table - nested for loop" to display specific numbers or a specific range of numbers. This can be done by modifying the conditions in the outer and inner loops, such as changing the start and end values or setting a step size. You can also add additional conditions or operations within the loops to further customize the output.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
Replies
10
Views
958
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top