Python Program to Divide Two Numbers using Function

We will develop a Python program to divide two numbers 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 numbers num1 and num2. Then, divide numbers using the division operator (/). We can take the help of a function to divide two numbers. A function is a block of code that performs a specific task.

How to divide two number:
Product = a / b

Mathematically,

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

How to Divide Two Numbers in Python using Function

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 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 numbers using function

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

 # take inputs
num1 = 25
num2 = 5

# 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 25 and 5 is 5.0

Python Program to Divide Two Numbers 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 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 for the different input values:-

Enter first number: 42
Enter second number: 6
The division of 42.0 and 6.0 is 7.0

Enter first number: 256
Enter second number: 31
The division of 256.0 and 31.0 is 8.26

Enter first number: 124
Enter second number: 3.2
The division of 124.0 and 3.2 is 38.75

Python Program to Divide Two Numbers using Recursion

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: 80
The division of 240 and 80 is 3

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

Leave a Comment

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