Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

How to create a multiplication table for any number in Python

Goodness Chidinma Abarugo

Overview

In Python, we can create a multiplication table for any number by combining the input() and range() functions with a loop statement.

The input() function

The input() function is used to accept input from the user. If the required data type is not explicitly defined, any value provided by the user at the prompt is stored in memory as a string.

Syntax

input(prompt)

Parameter

  • prompt: A string enclosed in single or double-quotes. Its presence makes our code more interactive. It is an optional parameter.

The range() function

The range() function allows us to create a series of numbers automatically. We iterate over the series while executing some lines of code until we arrive at the last number in the series.

Syntax

range(start, stop, step)

Parameters

  • start: The first number in the series. It is an optional parameter.

  • stop: The last number in the series. It is a required parameter.

  • step: The step size by which we want to increment or decrement our series. It is an optional parameter.

Note: If only one parameter is specified, that parameter is classified as the stop parameter, while the start and step parameters are taken as 0 and 1, respectively.

Loops

Loops are useful when we want to execute a line or block of code more than once, provided a condition is met or until we hit the last value in a series.

We’ll use for and while loops in this task. There is a slight difference between the for and while loop syntax. The for loop works within a range, and the while loop works only when one or more conditions are met.

Syntax

# for loop
for x in series:
   Do something

# while loop
while condition is met:
    Do something

Create the multiplication table

We’ll create our multiplication table based on the flowchart below:

Creating the multiplication table

Our flowchart above translates into the following algorithm:

  1. Start the program.
  2. Get an integer input from the user.
  3. Next, we’ll define a range or a condition. The range will be used in the for loop, and the condition will be used in the while loop.
  4. Finally, the code will test our item or condition. The code within the loop will continue to execute until items are outside the range or conditions are not met.

Code using a for loop

We’ll use the following code to generate a multiplication table using the for loop.

ourNum = int(input("Enter the number you want to generate a multiplication table for, then hit the `enter` key: "))
ourRange = range(1,6)
for x in ourRange:
    result = ourNum * x
    print(ourNum," * ",x," = ",result)
Multiplication table using the for loop

Explanation

Let’s explain our code:

  • In line 1, we request a number from the user. The multiplication table will be created for this number. We convert the number to an integer data type by enclosing our input() in an int().

  • In line 2, we define ourRange which includes numbers from 1 to 5.

  • Next, in line 3, we initiate our for loop and define x as a variable to hold the items stored in the ourRange variable.

  • In line 4, we use the result variable to hold the value of the product of the user given number and the current item in the range.

  • In line 5, we display the user given number, a multiplication sign, the current item in the series, an equals sign, and the value held by the result variable in each iteration.

Output

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Note: We require an integer from the user during code execution. Therefore, we’ll input 7 as the number we want to create a multiplication table for. In our output, the loop terminates after multiplying by 5 instead of 6. This is because the last item in a range is not used during code execution.

Code using a while loop

We’ll use the code below to generate a multiplication table using a while loop.

ourNum = int(input("Enter the number you want to generate a multiplication table for, then hit the `enter` key: "))
p = 1
while p < 6:
    result = ourNum * p
    print(ourNum, " * ", p," = ",result) 
    p = p + 1
Multiplication table using the while loop

Explanation

Unlike the for loop, our while loop needs a counter to make its iteration.

  • In line 1, we request an integer from the user.

  • In line 2, we set p as the variable that holds our counter, and set its initial value to 1.

  • In line 3, we state that the code block within our while loop will only execute as long as p is less than 6.

  • In line 4, we get the product of the number we input and the current number in the series.

  • In line 5, we display a row of our multiplication table.

  • In line 6, we increase our counter by 1.

Output

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Note: Our integer here is 7. We start our counter p from 1 and continue to increase it by 1 until we get a number that is equal to, or greater than 6.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Goodness Chidinma Abarugo
RELATED COURSES

View all Courses

Keep Exploring