Write a Python Function to Print Multiplication Table of a Given Number

Given an integer number  the input to the Python program your task is to write a Python program that accepts the integer and then calls the function to print the multiplication table of that particular number.

This program has just six lines of code so it's very easy, just keep reading…..

 

 

Write a Python Function to Print Multiplication Table of a Given Number

 

 

Write a Python Function to Print Multiplication Table of a Given Number

Let's  define a function named table and pass the integer argument as an input to the Python function.  Using the for loop, iterate from 1 to 11 and get the index values from 1 to 11.  inside the follow multiply the number with the index and print the the multiplication table of any given number as shown below

def table(n):
   for index in range(1,11):
       print(f"{n} x {index} = {n * index}")





In the main program take a variable named as number and use the input function call to ask a user to enter a number and then convert it into an integer using the type conversion int().

continue the main program to print the User friendly message that is multiplication table of a number is …  and then call the function named table() and pass the input argument number  to the function

number = int(input("Enter a Number : "))
print(f"Multiplication table of Number {number} is ... \n")
table(number)






Complete Program
===================

def table(n):
   for index in range(1,11):
       print(f"{n} x {index} = {n * index}")


number = int(input("Enter a Number : "))
print(f"Multiplication table of Number {number} is ... \n")
table(number)








OUTPUT
=============

Enter a Number : 5
Multiplication table of Number 5 is ...

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





Output
===================

Enter a Number : 12
Multiplication table of Number 12 is ...

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120




Output
======================

Enter a Number : 7
Multiplication table of Number 7 is ...

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70




Conclusion
==================

Execute the above program and provide the random integer as an input to the Python program.  note down the results

Comment down below if you have any suggestions or queries to improve the above program




Post a Comment

0 Comments