Python Divide Two Integers

We will develop a Python divide two integers. 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 numbers using the division operator (/). Here, we will divide two integers using various methods.

How to divide two integer:
Product = a / b

Mathematically,

Inputs: a=15, b=3
Product = a / b = 15 / 3 = 5

Python 2 Integer Division

This is the simplest and easiest way to divide two integers in Python. We will take two integers while declaring the variables and divide numbers. Its value will be stored in the division variable and finally, the division value will be displayed on the screen.

Program description:- Write a python program to find division between two integers

# Python program to divide two integers

# take inputs
num1 = 10
num2 = 2

# Divide numbers
division = num1/num2

# print value
print("The division of {0} and {1} is {2}"
               .format(num1,num2,division))

Output:-

The division of 10 and 2 is 5.0

Python Divide Two Integers

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.

Program description:- Write a program to perform division using variables in python

# Python program to divide two integers

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

# Divide numbers
division = 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: 45
Enter second number: 9
The division of 45 and 9 is 5.0

Enter first number: 24
Enter second number: 8
The division of 24 and 8 is 3.0

Divide Two Integers in Python using Function

We can also take the help of a function to divide two integers in python. A function is a block of code that performs a specific task.

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

Enter first number: 500
Enter second number: 50
The division of 500 and 50 is 10.0

Divide Two Integers Without using Division Operator in Python

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: 250
Enter second number: 5
The division of 250 and 5 is 50

Also See:- Python Program to Find Average of Two Numbers

Leave a Comment

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