Monday, March 11, 2024

BCA PART A 4. Display Multiplication Tables

 def multiplication_table(number, limit):

# Display the multiplication table for the given number up to the specified limit
print(f"Multiplication Table for {number} up to {limit}:")

for i in range(1, limit + 1):
result = number * i
print(f"{number} * {i} = {result}")

# Get input from the user
number = int(input("Enter the number for the multiplication table: "))
limit = int(input("Enter the limit for the multiplication table: "))

# Call the function to display the multiplication table
multiplication_table(number, limit)

Output :
Enter the number for the multiplication table: 5 Enter the limit for the multiplication table: 5 Multiplication Table for 5 up to 5: 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25

Explaination:

Function Definition:

This line defines a function named multiplication_table that takes two parameters: number (the base number for the multiplication table) and limit (the maximum value up to which the table will be displayed).

Display Header:

This line prints a header indicating that the multiplication table is for the specified number and will be displayed up to the specified limit.

Multiplication Table Loop:

This loop iterates from 1 to limit (inclusive) and calculates the result of multiplying the number by the loop variable i. It then prints each multiplication equation in the format "number * i = result".

User Input:

These lines prompt the user to enter the number for which they want to see the multiplication table and the limit up to which the table should be displayed. The inputs are converted to integers using int().

Function Call:

This line calls the multiplication_table function with the user-provided values of number and limit. The function then displays the multiplication table accordingly.

When you run this program, it will prompt you to enter a number and a limit. After entering the values, the program will display the multiplication table for the specified number up to the given limit.

No comments:

Post a Comment