about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

How to Print the Table of Any Number Program in C++?

To print the table of any number in a programming language is one of the basic tasks to perform to learn the basics of the programming language. So, if you are interested in how to print the table of any number using the C++ program, than you are at right place else you can leave this site to save your time.

Add caption

4 Proven Methods to Print the Table of a number in a C++ Program.

There are hundreds of dozens method/ways in programming to perform any task. Same as there are different methods to capture the same information either by watching lecture videos on YouTube, searching through different articles or blogs, skimming through the books and so on.

This example shows that you can perform the task of printing the table of a number in a multiple way. My personal opinion is that “If you are programmer and you don’t know how to perform a single programming task in multiple ways than you are a poor programmer”.

So let’s start to perform the same task of the printing the table on screen in different ways. Though there are hundreds of ways to do that but we will cover only those ways which are more efficient and reliable.

We have concluded the following 4 ways to print the table on the screen.

Method: 1

Using For Loop Number Entered By Programmer

This method is perhaps the easiest way to print the table on the screen. Some beginner students who want to print the table of any number think that this method is enough to print the table. But C++ has some other advanced and flexible features to help programmer to print the table. The code and the output related to this method are as under.

Code

#include<iostream>          // header file

using namespace std;          

int main()                           // main function

{

            int num=7;      //variable declaration and definition

            cout<<"Table of "<<num<<" "<<endl;               

            for(int i=1;i<=10;i++)                                   // for loop

            cout<<num<<" * "<<i<<" = "<<num*i<<endl;            // logic of displaying table

            return 0;

}

 

Output

Here is the sample output of this program.

 

 

 

 

 

Shows that the table of seven is printed on to the screen, when a user wants to print the table of another number, he will ask the programmer the number, programmer will change the code, and user desired number’s table will be print on the screen.

So there is a big problem that programmer had to change the code again and again as user demands change, such a practice will show that the programmer is servant of the user or user is completely dependent on the programmer so we need a middle way. And a middle way is method no 2.

Method: 2

Using for Loop Number Entered By User

Now in this method the whole scene will be changed. User will be free to enter the number as her choice with his own finger tips and will get the table of number. The programmer will once written the code and provide it to the user. This program will provide the user enough facility that each time they run the program, whatever number they enter, they will get the table of that number. The code and output of this C++ program is as under.

Code

#include<iostream>        //header file

using namespace std;

int main()                         //main function

{

            int num;                //variable declaration

            cout<<"Enter the number to find the table"<<endl;

            cin>>num;                     // taking values in variable

            cout<<"\n\nTable of "<<num<<" \n"<<endl;

            for(int i=1;i<=10;i++)               //for loop

            cout<<num<<" * "<<i<<" = "<<num*i<<endl;          //logic to display table

            return 0;

}

 

Output

Here is the sample output of this program.

 

 

 

 

 

Show the table of seven printed on the screen. Using this method user has full freedom to perform its operations. He will not tell the programmer what to do, he will do himself. If he need to print the table of 10, he will close the program execution, execute/run it again and will enter the 10 and program will display the table of 10. Though this method has ease for user, but not too much suppose a user had to print the 100 tables one after another. Is he will close and open the .exe file of program hundred time. Let’s discuss in method 3.

 

Method: 3

Using while and do while Loop Number Entered By User

If a user open and close the .exe file hundred time. He will never use the C++ program next time. To be this scenario in mind C++ developers gave the extra efficiency to C++. So in this method we will display the table in such a way that if a user prints hundred table he will open/run .exe file only once. The code is totally different from previous twos and the while and do while loops are used.

Code

#include<iostream>                 //header file

using namespace std;

int main()                                    //main function

{

            int num;                  //variable declaration

            char choice;             //variable declaration

            do{                      //do while loop

           

            cout<<"Enter the number to find the table"<<endl;

            cin>>num;                 //taking int type value

            cout<<"\n\nTable of "<<num<<" \n"<<endl;

            int i=1;

            while(i<=10)            //while loop

            {

           

            cout<<num<<" * "<<i<<" = "<<num*i<<endl;    //logic to display table on screen

            i++;

}

cout<<"Want to Find the Table of another number  (y/n)?"<<endl;

cin>>choice;                   //taking char type variable

}

while(choice=='y');

            return 0;

}

 

 

Output

Here is the sample output of this program.

 

 

 

 

 

Show that the user had displayed the table of 2, 3, 4 on the same screen entering the number. Using this method user can print the tables of hundreds or thousands of number’s tables on the same screen. Now we move on to the next method without any delay.

 

Method: 4

Using static while loop number Entered By programmer

This method is another way to print the table if you don’t like to print the table using the previous methods. This method is same like the method no 1 where we have use the for loop but here is the wile loop. The code and output for this method is as under.

 

 

Code

#include<iostream>        // header file

using namespace std;

int main()                          //  main function

{

            int num=100;  // variable declaration and definition

            cout<<"\n\nTable of "<<num<<" \n"<<endl;

            int i=1;

            while(i<=10)         // while loop

            {

           

            cout<<num<<" * "<<i<<" = "<<num*i<<endl;       // logic to display the table

            i++;

}

 

            return 0;

}

Output

Here is the sample output of this program.

 

 

 

Shows that the hundred is printed on the screen and if you need to print the table of two hundred you definitely need to contact programmer if you are not programmer.

 

 

NOTE:

Ø These programs are written and compile on the Dev C++ IDE. If you have some other compiler or IDE this code may generate errors. So In order to get the same output from them Download and Install Dev C++ IDE  and then run the code on it and get the exactly same output.

 

In Short

While learning to the code practice different and different ways to go deep in the deepest layer of code and perform the single task with at least 10 different ways. If you are interested in more ways to print the table comment us we will update 10 different ways to display the table.

ü  Also, if you have any type of bug or error in your code. Send us your code we will definitely help you to cope up from the error.

 

Thanks every one for reading this Blog.

Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents