Python program to multiply two numbers
In this post, you will learn different ways to write Python program to multiply two numbers. Such a type of question is generally asked in a programming interview. The interviewer can ask you to write a program to multiply two numbers in a particular way. Python enables us to write clear, logical applications for small and large tasks efficiently.
Simple Python program to multiply two numbers
In this program, you will learn how to multiply two numbers in Python in a simple way. First, the two numbers are stored in the variables x and y, respectively, and then, we use the multiplication operator (*) to multiply two numbers.
#Python program to multiply two numbers
x=32
y=19
mul = x * y;
print("Multiplication of two numbers: ",mul)
Output of the above code-
Multiplication of two numbers: 608
Multiplication of two numbers in Python using user inputs
In the given example, we first read two numbers from the user using a built-in function input() and store them in two variables, p and q respectively, and then, we use the multiplication operator (*) to multiply them.
p = int(input("Enter first number: "))
q = int(input("Enter second number: "))
result = p * q;
print("Multiplication of two numbers:", result)
Output of the above code-
Enter first number: 54
Enter second number: 36
Multiplication of two numbers: 1944
Multiplication of two numbers in Python using function
In the given Python program, we have defined a function multiplication to multiply two numbers. This program allows a user to enter two numbers. We will pass these two values as function arguments to calculate the multiplication in Python.
def multiplication(x, y):
z = x * y
return z
p = int(input("Enter first number: "))
q = int(input("Enter second number: "))
result = multiplication(p, q)
print("Multiplication of two numbers:", result)
Output of the above code-
Enter first number: 42
Enter second number: 21
Multiplication of two numbers: 882
Multiplication of two numbers in Python using recursion function
Here is the Python program to find the multiplication of numbers using the recursion function. When a user enters two numbers as input, it passes to the function. If the number equals 0, it returns 0.
def multiply(x,y):
if(x<y):
return multiply(y,x)
elif(y!=0):
return (x+multiply(x,y-1))
else:
return 0
x=int(input("Enter first number: "))
y=int(input("Enter second number: "))
print("Multiplication of numbers is ", multiply(x,y))
Output of the above code-
Enter first number: 15
Enter second number: 63
Multiplication of numbers is 945
Python program to multiply two float numbers
When we multiply one or both numbers of the float type using the asterisk character " * ", then the product is float number.
def multiplication(x, y):
z = x * y
return z
p = float(input("Enter first number: "))
q = float(input("Enter second number: "))
result = multiplication(p, q)
print("Multiplication of two numbers:", result)
Output of the above code:
Enter first number: 3.2
Enter second number: 7.2
Multiplication of two numbers: 23.040
Python program to multiply two complex numbers
In Python, we use the complex() method to multiply complex numbers, and the complex number contains real and imaginary parts.
num1 = complex(3, 5)
num2 = complex(4, 6)
product = num1 * num2
print('Multiplication of two complex numbers: ', product)
Output of the above code:
Multiplication of two complex numbers: (-18+38j)
Related Articles
Sum of digits of a number in Python
Multiply each element of a list by a number in Python
Find square of a number in Python
Python program to print all even numbers between 1 to 100
Eye Detection Program in Python OpenCV
Python OpenCV Overlaying or Blending Two Images
Find the stop words in nltk Python
Reverse pyramid pattern in Python
Fizzbuzz program in Python
Python split string by comma
Python alive progress bar
Python progress bar tqdm
Python language translator code
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Reverse a number in Javascript
HTML open link in new tab
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword