Q:

Python program to multiply two numbers

0

Python program to multiply two numbers

In this exercise, 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to remove last element from... >>
<< Write a python program to map two lists into a dic...