Python program to print table of number entered by user

Here, we are going to write a Python program to print table of number entered by user.
By Shivang Yadav Last updated : January 13, 2024

Problem statement

Input an integer number, write a Python program to print its table.

Table of a number is its multiples from 1 to 10.

Example

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Logic to print table of number entered by user

The steps to print the table of number entered by the user are:

  • Input a number by the user.
  • Initialize the loop counter (i) by 1.
  • Run a while loop till the loop counter is less than or equal to 10 (i <= 10).
  • Multiply the number with the loop counter (n * i).
  • Print the result.

Python program to print table of number entered by user

Input an integer number, print its table.

# Input a number
n = int(input("Enter The Number : "))

# Initialize loop counter by 1
i = 1

# Loop to print table
while i <= 10:
    # multiply number by loop counter
    t = n * i
    # print result
    print(n, "x", i, "=", t)
    # increase loop counter by 1
    i = i + 1

Output

The output of the above program is:

RUN 1:
Enter The Number : 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

RUN 2:
Enter The Number : 108
108 x 1 = 108
108 x 2 = 216
108 x 3 = 324
108 x 4 = 432
108 x 5 = 540
108 x 6 = 648
108 x 7 = 756
108 x 8 = 864
108 x 9 = 972
108 x 10 = 1080

Other Approaches

Print table by using the for loop

You can also use the for loop. Run a loop in the range of 1 to 11 by using the range(1, 11) method. This method will return the values from 1 to 10. Multiply them by the number to print the table terms.

Consider the below code:

# Input a number
n = int(input("Enter The Number : "))

# Loop to print table
for i in range(1,11):
    t = n * i
    print(n, "x", i, "=", t)

Print table by using recursion function

A recursion function is an approach to define a function in such a way that a function calls itself, you can also use the recursive approach to print a table of the given number.

Consider the below code:

# Recursion function to print the table of
# a given number
def print_table(num, i):
    if i > 10:
        return

    # Multiply number by 1, and print
    print(num, "x", i, "=", num * i)

    # Recursive call
    return print_table(num, i + 1)

# Main code
# Input a number
n = int(input("Enter The Number : "))
 
# Call the recursion function
print_table(n, 1)

Print table by list comprehension

The list comprehension approach is also useful to print a table. Run a for loop as an expression inside the list comprehension. This will print the multiple table terms as a list.

Consider the below code:

# Main code
# Input a number
n = int(input("Enter The Number : "))

# List comprehension to print table
result = [n * i for i in range(1, 11)]

# Printing the table
print("Table of", n, "is:\n", result)

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.