C++ | 2 Table using loops

Introduction

This tutorial will create 2 table using  loops in c++. The first program prints 2 to 20 using for loop. The next one uses while loop. The last one uses do-while loop. You can extend the program by just replacing the 10 by the limit. So enjoy the program. Lets begin………………

Program to print 2’s table using for loop

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//loop statement
for(i=2;i<=20;i++)
{
cout<<i<<endl;
i++;
}

//get character
getch();
}

Output

2
4
6
8
10
12
14
16
18
20

Program to print 2’s table using while loop

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//loop statement
i=2;

while(i<=20)
{
cout<<i<<endl;
i++;
i++;
}

//get character
getch();
}

Output

2
4
6
8
10
12
14
16
18
20

Program to print 2’s table using do-while loop

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//loop statements

i=2;
do
{
cout<<i<<endl;
i++;
i++;
}while(i<=20);
//get character
getch();
}

Output

2
4
6
8
10
12
14
16
18
20

How does it work

The table are directly printed. no need to input.

Extending it

The program can be extended by replacing the 20 by the limit in the program. You can also let user enter the limit that can be saved in a variable and replaces 20 in the program. Go ahead and extend it…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variable as int (name them as you want.)
  5. Add the loop.
  6. Print the number.

At the end

You learnt creating the c++ program of printing 2’s table from 2 to 20 using loops. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s