How do you write a program to multiply two numbers in python?

Problem Definition

Create a Python program to multiply two numbers.

Program

num_1 = 2
num_2 = 3
product = num_1 * num_2
print("Product of {} and {} is {}".format(num_1, num_2,product))

Output

Product of 2 and 3 is 6

First, the two numbers are stored in the variables num_1 and num_2, respectively. Multiplication in Python is done by ( * ) operator, Then the result is saved in the product variable and printed out using string formatting.

To learn more about string formatting in Python read - How To Use String Formatting In Python

However, the more memory-efficient way to perform a multiplication in Python is by not using any variables at all it can be done in a line, but it can make the code hard to read.

print(2*3)

Output

6

Problem Definition

Create a Python Program to multiply two numbers provided by the user in real-time.

Program

num_1 = input("Enter the first number")
num_2 = input("Enter the second number")

product = float(num_1) * float(num_2)

print("Product of {} and {} is {}".format(num_1, num_2,product))

Output

Enter the first number 2
Enter the second number 3
Product of 2 and 3 is 6.0

In this program first, we are taking user input with the built-in input() function of Python then before multiplying the numbers we are converting the inputs to float type using the float() function because the input() function returns the object as a string.

To learn more about Python data types read the following articles.

  • Data Types In Python
  • How To Convert Data Types in Python

PROGRAMS

In this post, you will learn different ways to write a 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.

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 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 using function

In the given 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 numbers 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

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)

How do you make a multiplication program in Python?

Method 2: By using While Loop.
number = int(input ("Enter the number of which the user wants to print the multiplication table: ")).
count = 1..
# we are using while loop for iterating the multiplication 10 times..
print ("The Multiplication Table of: ", number).
while count <= 10:.
number = number * 1..

How do you multiply a variable by a number in Python?

However, in most programming languages, including Python, we use the * (asterisk) sign to multiply variables instead of ×. So, to take the product of two variables, we use x*y.

How do you multiply in code?

Program to Multiply Two Numbers printf("Enter two numbers: "); scanf("%lf %lf", &a, &b); Then, the product of a and b is evaluated and the result is stored in product . product = a * b; Finally, product is displayed on the screen using printf() .

How do you multiply a list by 2 elements in Python?

The zip() function in python can combine the contents of 2 or more iterables. Zip Function returns a zipped output. We can then simply store the output in Result and Display it on the console. This is a very simple way to perform list multiplication in Python.