Python Program to Print a Table of Given Number

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

1. Introduction

In this tutorial, we will learn how to write a Python program to print a table of a given number.

The multiplication table of a number is a foundational concept in arithmetic and a common programming task that can be used to teach loops and output formatting in Python. Creating a table for a number involves multiplying that number by a series of consecutive integers.

A multiplication table for a number displays that number multiplied by a range of other numbers, usually from 1 to 10. Each line of the table shows the result of the number multiplied by each of these integers in succession.

2. Program Steps

1. Choose a number to generate the table for.

2. Decide the range of multiples (commonly 1 to 10).

3. Loop through the range, multiplying the chosen number by each integer in the range.

4. Print the results in a formatted manner.

3. Code Program

# Function to print the multiplication table of a number def print_table(number): # Define the range of multiples for i in range(1, 11): # Calculate the product product = number * i # Print the formatted multiplication line print(f"{number} x {i} = {product}") # Choose a number num = 5 # Call the function to print the table print_table(num) 

Output:

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 

Explanation:

1. A function called print_table is defined with a parameter number, which is the number for which the table will be generated.

2. A for loop iterates through the range 1 to 10, representing the multiples.

3. In each iteration, the product of the number and the loop counter i is calculated.

4. The multiplication fact is printed out in a formatted string using Python's f-string notation f"{number} x {i} = {product}".

5. After defining the function, it is called with num as the argument, which is set to 5.

6. The function prints out the multiplication table for 5, from 5 x 1 up to 5 x 10.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare