If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To generate a table (multiplication table) of a given number in Python, you can use a
for
loop to iterate from 1 to a specified range and print the result of the multiplication. Here's how you can create a table for a given number:pythonCopy code
def generate_table(number, table_length): for i in range(1, table_length + 1): result = number * i print(f"{number} x {i} = {result}") # Example usage: num = 5 length = 10 generate_table(num, length)
In this example, the
generate_table()
function takes two parameters:number
, which is the number for which you want to generate the table, andtable_length
, which is the number of rows you want in the table.The
for
loop runs from 1 totable_length + 1
(inclusive) using therange()
function. For each iteration of the loop, it calculates the result of the multiplication (number * i
) and prints it in the format "number x i = result."When you call the
generate_table()
function withnum = 5
andlength = 10
, it will generate a table for the number 5 with 10 rows, showing the multiplication of 5 with numbers from 1 to 10.Output:
Copy code
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
You can adjust the value of
num
andlength
to generate tables for different numbers and with different lengths as needed.
Comments: 0