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