We will develop a Python divide two integers using Function. The division is one of the four basic mathematical operations, the other three being addition, subtraction, and multiplication. In simple words, division can be defined as the splitting of a large group into equal smaller groups.
We will give two integers num1 and num2. Then, divide integers using the division operator (/). We can take the help of a function to divide two integers. A function is a block of code that performs a specific task.
How to divide two integers:
Product = a / b
Mathematically,
Inputs: a=35, b=5
Product = a / b = 35 / 5 = 7
Python 2 Integer Division using Function
This is the simplest and easiest way to divide two integers in Python. We will take two integers while declaring the variables and divide integers using a user-defined function. Its value will be stored in the division variable using a function call and finally, the division value will be displayed on the screen.
# Python program to divide two integers using function
def div_Num(num1, num2): #user-defined function
div = (num1/num2) #divide numbers
return div #return value
# take inputs
num1 = 50
num2 = 25
# function call
division = div_Num(num1, num2)
# print value
print("The division of {0} and {1} is {2}"
.format(num1,num2,division))
Output:-
The division of 50 and 25 is 2.0
Python Divide Two Integers using Function
In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user. Inputs are scanned using the input() function and stored in variable num1, and num2.
# Python program to divide two integers using function
def div_Num(num1, num2): #user-defined function
div = (num1/num2) #divide numbers
return div #return value
# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
# function call
division = div_Num(num1, num2)
# print value
print("The division of {0} and {1} is {2}"
.format(num1,num2,division))
Output for the different input values:-
Enter first number: 1000
Enter second number: 100
The division of 1000 and 100 is 10.0
Enter first number: 200
Enter second number: 4
The division of 200 and 4 is 50.0
Divide Two Integers in Python using Recursion
In this program, divide two integers using recursion. A function/method that contains a call to itself is called the recursive function/method. A technique of defining the recursive function/method is called recursion.
# Python program to divide two integers using recursion
def div_Num(x,y): #user-defined function
if (y==0):
return 0;
elif (x-y==0):
return 1;
elif (x<y):
return 0;
else:
return (1+div_Num(x-y,y));
# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
# function call
division = div_Num(num1, num2)
# print value
print("The division of {0} and {1} is {2}"
.format(num1,num2,division))
Output:-
Enter first number: 600
Enter second number: 30
The division of 600 and 30 is 20