C++ Program to Print Multiplication Table Using For Loop

In this tutorial, I will show you that how you can make a C++ Program to Print Multiplication Table Using For Loop.

Program Logic and Structure:

Multiplication table is a set of multiples of particular number typically from 1 to 10. This table is generated by multiplying that particular number with numbers (from 1 to 10) one by one.

We will do same but with code. We will create a variable to store user’s desired number. This number would be multiplied with loop variable to generate table of given number.

Will we write multiplication expression 10 times?

No, we will use FOR loop to do multiplication many times. FOR loop comes handy when the number of repetitions are known. As we know that we want to repeat multiplication 10 times, so we will use this loop structure.

 Code to Print Multiplication Table Using For Loop:

#include<iostream>
using namespace std;
int main(){
	int num;
	cout<<"ENTER A NUMBER: ";
	cin>>num;
	for(int i=1;i<=10;i++)
  {
	    cout<<num<<"*"<<i<<"="<<num*i<<endl;
	}
	return 0;
}

If you are using Dev C++ then press F11 to compile and execute this program. Here is the output of program:

Multiplication Table Program Using For Loop in C++

 

How Program works:

Below is the basic structure of a C++ program. No matter, how complicated or simple a program is, every C++ program have these lines.

#include<iostream>
using namespace std;
int main()
{
      return 0;
}

I have explained these lines fully in my article Here.

Now, I will explain rest of the lines in details.

Line 4:

int num;

In this line, we declared a variable named num. This variable is used to store the number entered by user. Keyword int shows that this variable has integer data type. We used this type because we want to store numeric data in this variable. At the end of line, there is a semi-colon ( ; ) which terminates this statement.

Line 5:

cout<<"ENTER A NUMBER: ";

In this line, we prompted the user to enter a number. cout statement is used to print something on screen. This statement prints the text which is written in double quotations( ” “ ) on the screen. In this case, it will print “Enter a number: “ without double quotations.

Line 6:

cin>>num;

The basic purpose of cin statement is to collect input from user. This statement keeps record of keys pressed by user on key-board and stores value in the variable coming after extraction operator ( >>). In this case, it will save the number entered by user in variable num. Let’s assume that number entered  by user is 5.

Line 7:

for(int i=1;i<=10;i++){

Here FOR loop is started. i is a loop control variable and initialized to 1. After executing statements first time, its value is incremented and becomes 2. After that, Condition is checked and it is satisfied, so statements of body executes once more. This loop is repeated until i reaches 10. When it is greater than 10, condition becomes false and loop ends.

If you want to increase range of table then replace ’10’ with any other number you want.

Line 8:

cout<<num<<"*"<<i<<"="<<num*i<<endl;

Basically this line makes the output of program. This line is iterated ten times because it lies inside the body of FOR loop. Three values are same in every line i.e number, asterisk( * ) and equal sign( = ).  num*i is an expression and its value depends upon i. The value of i is incremented every time loop repeats itself. At first, value of i is 1 so it shows

5*1=5

When loop repeats then i becomes 2, so

5*2=10

When loop runs again then i becomes 3. So

5*3=15

And it is repeated until reaches to 10.

Line 9:

} 

Here closing curly brace ( } ) ends the body of FOR loop.

Rest of the lines have been explained in my article:  Introduction of C++ Language & C++ Hello World Program.

This was all about multiplication table program in C++ programming language.

Thanks for reading this article. If you have any question then you can ask me in comments.

You can also Click Here to Download Exercise Files.