def table(number, limit=10):
print(f"Multiplication table of {number}:")
for i in range(1, limit + 1):
print(f"{number} x {i} = {number * i}")
number = int(input("Enter a number: "))
table(number)
Code Explanation:
1. Function Definition
def table(number, limit=10):
print(f"Multiplication table of {number}:")
Parameters:
number: The number for which the multiplication table is generated.
limit: The maximum value up to which the multiplication table is calculated (default is 10).
Header Message:
The print statement outputs a message indicating the multiplication table being generated.
2. Loop to Generate Multiplication Table
for i in range(1, limit + 1):
print(f"{number} x {i} = {number * i}")
How It Works:
range(1, limit + 1) generates integers from 1 to limit (inclusive).
The loop iterates through each number i in this range.
In each iteration:
The current value of i is multiplied by number.
The result (number * i) is formatted and printed in the form:
number x i = result
3. User Input and Function Call
number = int(input("Enter a number: "))
table(number)
Input:
The user is prompted to enter an integer (number).
int(input(...)) converts the input string into an integer.
Function Call:
The table function is called with number as the argument. Since the limit parameter has a default value of 10, it does not need to be explicitly specified unless a custom limit is required.
#source code --> clcoding.com
0 Comments:
Post a Comment