Arithmetic Caculation

Python program to Multiply two integers|in 5ways

Python program to Multiply two integers|in 5ways

In this tutorial, we will discuss the Python program to Multiply two integers|in 5ways

In this post, we are going to learn how to find multiplication of two numbers via different 5 ways in Python language

Program to Multiply two integers

Program to Multiply two integers – standard method

Program 1

#Python program to mutipy two numbers
num1=15
num2=16
mul=num1 * num2;
print("Multiplication of two numbers: ",mul)

When the above code is executed, it produces the following result

Multiplication of two numbers:  240
  • Integer variable num1 and num2 both are declared and initialized
  • the num1 and num2 both are multiplied together and the value is assigned to the integer variable mul
  • finally the program is displayed the output using print() function

Program to Multiply two integers – using user input

Program 2

#Python program to mutipy two numbers
num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))
#input numbers from the user

mul=num1 * num2;#caculate multipication

print("Multipication of two numbers: ",mul)
#display output

When the above code is executed, it produces the following result

Enter first number: 12
Enter second number: 24
Multiplication of two numbers:  288

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input and store the variables in num1, num2.
  • the num1 and num2 both are multiplied together and the value is assigned to the integer variable mul
  • finally the program is displayed the output using print() function

 

Program to Multiply two integers – using function

Program 3

#Python program to multiply two numbers using function

def multiply(num1,num2):#function definition
    product=num1 * num2
    return product

num1=int(input("Enter first number "))
num2=int(input("Enter second number "))
#input numbers from the user

result=multiply(num1,num2)#caling the function

print("Multipication of two numbers: ",result)
#display output

When the above code is executed, it produces the following result

Enter first number 12
Enter second number 21
Multiplication of two numbers:  252

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input  store the variables in num1, num2.
  • define the function for find product of two numbers
  • When we call the function, the num1 and num2 both are multiplied together
  • Then the value is assigned to the integer variable result
  • finally the program is displayed the output using print() function

 

Program to Multiply two integers – using recursion

Program 4

def product(x,y):
    if(x<y):
        return product(y,x)
    elif(y!=0):
        return (x+product(x,y-1))
    else:
        return 0

x=int(input("Enter first number: "))
y=int(input("Enter second number: "))

print("Product is ", product(x,y))

When the above code is executed, it produces the following result

Enter first number: 100
Enter second number: 50
('Product is ', 5000)

Approach

  • Integer variable x and y both are declared,
  • The program takes input from the user
  • Then the user enters the input value for x and y
  • the program will read the input  store the variables in  x , y.
  • define the recursive function for multiply two numbers
  • When we call the function, the x and y both are multiplied recursively
  • finally the program is displayed the output using print() function

 

Program to Multiply two integers – using without * operator

Program 5

num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))
product=0
for i in range(1,num2+1):
    product=product+num1
print("Product is ", product)

When the above code is executed, it produces the following result

Enter first number: 50
Enter second number: 60
Product is 3000

 

Suggested post

Function in Python language

Operator in Python language

Data type in Python

 

 

Similar post

Find product of two numbers in Java language|5ways

Find product of two numbers in C language|6ways

Find product of two numbers in C++ language|6ways

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.