Producing Multiplication Tables in C++
On this tutorial, we are going to present you the best way to generate multiplication tables in C++. A person is requested to enter the quantity for which he/she needs to generate the desk.
Totally different logics are used to generate the desk to 10 and past.
C++ Multiplication desk – Program 1 utilizing whereas loop
On this C++ program, we used some time loop to generate the desk of an entered quantity by the person. As you run this program, it can ask you to enter the quantity that you simply wish to generate the multiplication desk for.
After getting into the quantity, it can generate a desk until 10. Take a look on the code and some pattern outputs:
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
|
#embrace<iostream>
utilizing namespace std;
int principal() {
int x; int tab_num;
//Taking person enter for the quantity
cout<<“Please enter a quantity to generate its desk: “;
cin>>tab_num;
//Utilizing whereas loop to iterate until 10
x = 1;
whereas (x <= 10) {
cout<<tab_num<<” * “<<x<<” = “<<tab_num*x<<“n”; x++; }
return 0;
} |
Desk of 5:
Desk of 10
Desk of 25
Program 2 – Utilizing for loop to generate the multiplication desk
Now, we used a for loop to generate the desk. See the code and output:
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
|
#embrace<iostream>
utilizing namespace std;
int principal() { int x; int tab_num;
//Taking person enter for the quantity
cout<<“Please enter a quantity to generate its desk: “;
cin>>tab_num;
//Utilizing for loop to iterate until 10
for(int x=1;x<=10;x++) {
cout<<tab_num<<” * “<<x<<” = “<<tab_num*x<<endl; } return 0;
} |
Desk of 5:
Wish to transcend multiplying by 10?
The next instance multiplies the entered quantity for the desk to the given quantity. Within the above examples, we multiplied every given quantity to the vary of 10.
On this instance, the person is requested to enter the given quantity for the desk. It additionally asks to specify the vary the place it ought to multiply after which it can show the desk. Take a look:
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
|
#embrace <iostream>
utilizing namespace std;
int principal()
{
int tab_num, to_range, x;
//Taking person enter each numbers
cout << “Please enter a quantity to generate the desk: “;
cin >> tab_num;
cout << “Please enter the vary like 10, 15, 20? “; cin >> to_range;
//Utilizing for loop generate the desk
for (int x = 1; x <= to_range; ++x) {
cout << tab_num << ” * “ << x << ” = “ << tab_num * x << “n”;
}
return 0;
} |
Desk of 5 until 22: