Table Printing: Introducing the Range

Learn to print tables or sequences within the given range.

This lesson builds on the last lesson on loops and introduces several sentinel conditions on loops to solve different kinds of scenarios. Let’s look at them.

Printing the table up to a certain limit

You may have noticed that so far in our programs when wanting to set a limit, we’ve had to keep changing the code. For example, if we required the multiplication table up to the 20th or 30th multiple, we would need to change the code again.

Look at the following example to see how we can handle this issue.

The “Run” button is disabled by default because this program requires two integers as inputs. When entering multiple inputs, we enter each input, followed by a space or an enter key, and then the next input.

When you’ve entered all inputs, click the “Run” button.

Press + to interact
#include <iostream>
using namespace std;
//Table printing up to the limit(multiples) entered by user
//For loop that print table according to user input
int main()
{
int Table;
int Limit;
cout << "Which Table: " ;
cin >> Table; //Let us say user entered 5
cout << Table<<endl;
cout << "Limit: " ;
cin >> Limit; //let us say user entered 16
cout << Limit<<endl;
for(int i = 1; i <= Limit; i++)
{
cout << Table << " x " << i << " = " << Table*i << endl;
}
return 0;
}

Enter the input below

Instruction: Write two space-separated integers in the text box above, like 5 16. Note that during the execution of the code, the first number,5, will automatically be received in Table and the second number,16, will be received in Limit. You may try different values and run the code to see how the output comes.

We can see how easily we removed that issue of changing the code. We did this by taking a variable, Limit, and instead of hard coding the value of 16, we set the limit to the variable value, which was taken from the user.

Printing the table within a range (using for loop)

Instead of just taking the end limit of the table, we can ask the user for the start limit as well. The program to print a table within a specific range would then take one more input (Start) from the user.

Let’s take a look at the program below:

Press + to interact
#include <iostream>
using namespace std;
//Table printing within the user entered limit(multiple)
/*example of for loop that print table according to
user input within given limits*/
int main()
{
int Table;
int Start, End;
cout << "Table: ";
cin >> Table; //Let say user entered 7
cout << Table << endl;
cout << "Start: ";
cin >> Start; //Let say user entered 8
cout << Start << endl;
cout << "End: ";
cin >> End; //Let say user entered 15
cout << End << endl << endl;
// printing should begin from Start till End
for(int i=Start; i<=End; i++)
{
cout << Table << " x " << i << " = " << Table*i << endl;
}
return 0;
}

Enter the input below

Instruction: Add three integers as Table, Start, and End and see how the program outputs.

You should run the code above and observe the following:

  • Instead of one limit, we now have two limiting variables, Start and End.
  • After taking the input, instead of starting the counting variable i from 1, we started with the Start variable. Therefore, i is initialized with whatever value the user wants to start the range from.
  • The stopping criteria of i is bound by the End variable so that the table range does not exceed End.

This completes our first program of table printing.


summary

So far, we’ve tried to solve the simple problem of printing tables of numbers using various approaches.

We started with printing the table line-by-line using cout, and realized a drawback that changing the code was a cumbersome process. Next, we learned about variables that minimized the effort required to edit the code. Then, by connecting the variables with the input stream cin, we made it much easier and added more flexibility for the user. In the end, for scalability, we learned about loops that allowed us to avoid writing many lines of code. Lastly, we achieved the goal of making our program print within certain ranges.

Note that we were able to massively improve the concision of our code as well as make it more abstract using already existing constructs such as cout/cin, variable expression evaluators, while/for loops, etc. In effect, these constructs allow us to code without worrying how the computer hardware executes them. We just need to know how to use them instead of creating the already built constructs. We can build great programs using the work of the giants who created C++ and other programming languages and compilers. As Isaac Newton said “If I have seen further, it is by standing on the shoulders of giants,” so too are we able to do this because of the great computer scientists that have preceded us.

Our journey of seeing further has just begun!