How to create a multiplication table in C++
This is a C++ tutorial where we will learn how to create a multiplication table. We will accomplish this using a for loop in C++, however you can use while or do while loop also. Multiplication table for a number is often written till 10 but in this tutorial, we will do it for a specified range input too.
Creating a multiplication table in C++
Here we can create two types of multiplication table:
- Without range
- With a given range
Let’s explore one by one
If the range is not given:
If there is only one input specifying the number for which we have to create the multiplication table, then we will follow the below process:
- Accept the input (Integer) from the user and store it in a variable.
- Create a for loop and iterate it 10 times.
- For every iteration, multiply the variable with the iterating integer and print the output. Make sure the iterating integer starts with 1 and iterates 10 times.
Here is the code: Multiplication table in C++
#include <iostream> using namespace std; int main() { int i,a; cin>>a; for(i=1;i<=10;i++) { cout<<a<<" x "<<i<<" = "<<(a*i)<<endl; } return 0; }
Output:
5 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Explanation:
In this example code, input given by the user is 5 and it is stored in the variable a. In the next step, we have created a for loop with iterating integer i which is starting from 1. The loop ends when the condition (i<=10) is not satisfied that is the statement inside the loop is executed only till (i=10).
If the range is given: Create a multiplication table in C++
If the range is given for the multiplication table then, we will take two integer inputs from the user and based on that we will show the output.
- The first input should be the number for which we have to create the multiplication table.
- The second input should be a number that specifies the range of the table.
- Again, we need to create a for loop and iterate it as many times as the value given by the second input.
- For every iteration, multiply the first input with iterating integer and print the output. Make sure iterating integer starts with 1.
Multiplication table in a given range in C++
#include <iostream> using namespace std; int main() { int i,a,b; cin>>a; cin>>b; for(i=1;i<=b;i++) { cout<<a<<" x "<<i<<" = "<<(a*i)<<endl; } return 0; }
Suppose the input given by the user is this:
3 7
Then the output will be like this:
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21
Explanation:
Here, the user has entered two inputs. 3 and 7. 3 is stored in variable a and 7 is stored in variable b. Then, we have used a for loop with its iterating integer starting with 1. Now, the statement inside the loop is executed as long as the condition (i<=7) is satisfied that is multiplication operation is done only till (i=7).
how we can adjust its starting range? if I start it from any number for (from 5)
Set the starting range with input variable