Python Program to Display the multiplication Table

The provided Python program is designed to display the multiplication table for a specified number up to a given limit. It begins by prompting the user to enter a number for which they want to generate the multiplication table. Subsequently, the program requests the user to input the number of times they wish to display in the table. The core functionality is encapsulated in the multiplication_table function, which takes the user-provided number and the specified limit as parameters. Inside the function, a loop iterates from 1 to the specified limit, calculating the product of the input number and the current iteration. The result is then printed in a formatted manner, showing the multiplication table entries. Users can interact with the program by entering their desired number and the limit, and the program will output the corresponding multiplication table.


Source code

def multiplication_table(number, n):
    print(f"Multiplication Table for {number} up to {n} times:")
    for i in range(1, n+1):
        result = number * i
        print(f"{number} x {i} = {result}")

# Input the number for which you want to display the multiplication table
num = int(input("Enter a number: "))

# Input the number of times you want to display the multiplication table
times = int(input("Enter the number of times: "))

# Call the function to display the multiplication table
multiplication_table(num, times)

Description

Certainly! Let’s break down the provided Python program step by step:

  1. Function Definition: def multiplication_table(number, n): Here, a function named multiplication_table is defined, which takes two parameters: number (the base number for the multiplication table) and n (the number of times the table should be displayed).
  2. Print Statement: print(f"Multiplication Table for {number} up to {n} times:") This line prints a descriptive message indicating which multiplication table is being displayed, based on the user input for number and n.
  3. For Loop: for i in range(1, n+1): This line initiates a for loop that iterates over the range from 1 to n+1. The loop variable i represents the current iteration.
  4. Multiplication Calculation: result = number * i Inside the loop, the program calculates the product of the number and the current value of i to get each entry in the multiplication table.
  5. Print Multiplication Table Entry: print(f"{number} x {i} = {result}") This line prints each multiplication table entry in a formatted manner, showing the multiplication operation and the result.
  6. User Input: num = int(input("Enter a number: ")) times = int(input("Enter the number of times: ")) The program prompts the user to input the base number (num) for the multiplication table and the number of times (times) they want the table to be displayed.
  7. Function Call:
    python multiplication_table(num, times)
    Finally, the program calls the multiplication_table function with the user-provided values for num and times, executing the logic within the function to display the multiplication table.

Users can run this program, input the desired values when prompted, and observe the multiplication table for the specified number up to the specified limit.


Screenshot


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *