In the previous article, we have discussed Python Program to Swap Two Numbers using Bitwise Operators
Given two numbers and the task is to multiply the given two numbers without using multiplication(*) Operator
Examples:
Example1:
Input:
Given First Number = 3 Given Second Number = 6
Output:
The multiplication of given two numbers{ 3 * 6 } = 18
Example2:
Input:
Given First Number = 15 Given Second Number = 4
Output:
The multiplication of given two numbers{ 15 * 4 } = 60
Program to Multiply Two Numbers Without Using Multiplication(*) Operator in Python
Below are the ways to multiply the given two numbers without using multiplication(*) Operator in Python:
Method #1: Using For Loop (Static Input)
Approach:
- Give the first number as static input and store it in a variable.
- Give the second number as static input and store it in another variable.
- Take a variable say rslt_sum and initialize its value to 0.
- Loop from 1 to the given first number using the for loop.
- Inside the loop, add the given second number to the above-initialized variable rslt_sum and store it in the same variable rslt_sum.
- Print the variable rslt_sum to get the multiplication of given two numbers without using the multiplication(*) Operator.
- The Exit of the Program.
Below is the implementation:
# Give the first number as static input and store it in a variable. fst_numb = 3 # Give the second number as static input and store it in another variable. scnd_numb = 6 # Take a variable say rslt_sum and initialize its value to 0. rslt_sum = 0 # Loop from 1 to the given first number using the for loop. for itr in range(1, fst_numb+1): # Inside the loop, add the given second number to the above-initialized variable # rslt_sum and store it in the same variable rslt_sum. rslt_sum = rslt_sum+scnd_numb # Print the variable rslt_sum to get the multiplication of given two numbers without # using the multiplication(*) Operator. print("The multiplication of given two numbers{", fst_numb, "*", scnd_numb, "} = ", rslt_sum)
Output:
The multiplication of given two numbers{ 3 * 6 } = 18
Method #2: Using For loop (User Input)
Approach:
- Give the first number as user input using the int(input()) function and store it in a variable.
- Give the second number as user input using the int(input()) function and store it in another variable.
- Take a variable say rslt_sum and initialize its value to 0.
- Loop from 1 to the given first number using the for loop.
- Inside the loop, add the given second number to the above-initialized variable rslt_sum and store it in the same variable rslt_sum.
- Print the variable rslt_sum to get the multiplication of given two numbers without using the multiplication(*) Operator.
- The Exit of the Program.
Below is the implementation:
# Give the first number as user input using the int(input()) function and # store it in a variable. fst_numb = int(input("Enter some random number = ")) # Give the second number as user input using the int(input()) function and # store it in another variable. scnd_numb = int(input("Enter some random number = ")) # Take a variable say rslt_sum and initialize its value to 0. rslt_sum = 0 # Loop from 1 to the given first number using the for loop. for itr in range(1, fst_numb+1): # Inside the loop, add the given second number to the above-initialized variable # rslt_sum and store it in the same variable rslt_sum. rslt_sum = rslt_sum+scnd_numb # Print the variable rslt_sum to get the multiplication of given two numbers without # using the multiplication(*) Operator. print("The multiplication of given two numbers{", fst_numb, "*", scnd_numb, "} = ", rslt_sum)
Output:
Enter some random number = 15 Enter some random number = 4 The multiplication of given two numbers{ 15 * 4 } = 60
Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.