Python Program to multiply Two Numbers Without using * Operator

We will develop a python program to multiply two numbers without using * operator. We will give two numbers num1 and num2. Python programs will multiply these numbers using a For Loop. We will also develop a  python program to multiply two numbers using recursion.

How to find the product of two number:
Product = a x b

Mathematically,

Inputs: a=7, b=2
Product = a x b = 7 x 2 = 14

Multiply Two Numbers in Python Without using * Operator

We will take two numbers while declaring the variables. Then, calculate the product of numbers using the For Loop. Finally, the product of numbers will be displayed on the screen.

# Python program to multiply two numbers without using * operator

# take inputs
num1 = 3
num2 = 4

# calculate product
product = 0
for i in range(1,num2+1):
    product=product+num1

# print multiplication value
print("The product of number:", product)

Output:-

The product of number: 12

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

# Python program to multiply two numbers without using * operator

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calculate product
product = 0
for i in range(1,num2+1):
    product=product+num1

# print multiplication value
print("The product of number:", product)

Output for the input values test-case-1:-

Enter first number: 5
Enter second number: 7
The product of number: 35

Output for the input values test-case-2:-

Enter first number: 25
Enter second number: 36
The product of number: 900

Python Program to Multiply Two Numbers Without using * Operator

This python program also performs the same task but in different ways. In this program, we will find the product of two numbers using recursion. We can take the help of a function to multiply two numbers. A function is a block of code that performs a specific task.

# Python program to multiply two numbers without using * operator

def product_num(a,b):   #user-defined function
   if(a<b):
      return product_num(b,a)
   elif(b!=0):
      return(num1+product_num(a,b-1))
   else:
      return 0

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# function call
product = product_num(num1, num2)

# print multiplication value
print("The product of number:", product)

Output for the input values test-case-1:-

Enter first number: 12
Enter second number: 3
The product of number: 36

Output for the input values test-case-2:-

Enter first number: 56
Enter second number: 32
The product of number: 1792

Leave a Comment

Your email address will not be published. Required fields are marked *