Python Program to Divide Two Numbers

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

How to divide two number:
Product = a / b

Mathematically,

Inputs: a=10, b=2
Product = a / b = 10 / 2 = 5

How to Divide Two Numbers in Python

This is the simplest and easiest way to divide two numbers in Python. We will take two numbers 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 numbers

# Python program to divide two numbers

# 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 Program to Divide Two Numbers

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 numbers

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(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: 24
Enter second number: 8
The division of 24.0 and 8.0 is 3.0

Enter first number: 25
Enter second number: 2
The division of 25.0 and 2.0 is 12.5

Enter first number: 45
Enter second number: 3.2
The division of 45.0 and 3.2 is 14.0625

Division of Two Numbers in Python using Function

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

# Python program to divide two numbers using function

def div_Num(num1, num2):  #user-defined function
    div = (num1/num2)   #divide numbers
    return div   #return value

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(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: 100
Enter second number: 100
The division of 100.0 and 100.0 is 1.0

Divide Two Numbers Without using Division Operator in Python

In this program, divide two numbers 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 numbers 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: 240
Enter second number: 30
The division of 240 and 30 is 8

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

Leave a Comment

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