Python Program to Subtract Two Numbers using Function

We will develop a python program to subtract two numbers using function. We will give two numbers num1 and num2. Python programs will subtract these numbers using the arithmetic operator (-). We will also develop a python program to subtract two numbers without using the (-) operator.

Subtraction of Two Numbers in Python using Function

We can take the help of a function to subtract two numbers in python. A function is a block of code that performs a specific task. We will take two numbers while declaring the variables. Then, call the function and the subtraction of numbers will be displayed on the screen.

Program description:- write a program to subtract two numbers in python using function

# Python program to subtract two numbers using function

def subtract_num(a,b):   #user-defined function
   subtract = a - b   #subtracting numbers
   return subtract   #return value

# take inputs
num1 = 8
num2 = 3

# calling function
subtract = subtract_num(num1, num2)

# print subtraction of numbers
print('The subtraction of numbers {0} and {1} is {2}'
                         .format(num1, num2, subtract))

Output:-

The subtraction of numbers 8 and 3 is 5

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

Program description: Python program to subtract two numbers with user input

# Python program to subtract two numbers using function

def subtract_num(a,b):   #user-defined function
   subtract = a - b   #subtracting numbers
   return subtract   #return value

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

# calling function
subtract = subtract_num(num1, num2)

# print subtraction of numbers
print('The subtraction of numbers {0} and {1} is {2}'
                         .format(num1, num2, subtract))

Output for the input values test-case-1:-

Enter first number: 25
Enter second number: 8
The subtraction of numbers 25.0 and 8.0 is 17.0

Output for the input values test-case-2:-

Enter first number: 35.2
Enter second number: 18
The subtraction of numbers 35.2 and 18.0 is 17.2

Output for the input values test-case-3:-

Enter first number: 89.4
Enter second number: 32.06
The subtraction of numbers 89.4 and 32.06 is 57.34

Python Program to Subtract Two Numbers using Function

In this program, we subtract two numbers using a built-in function. The numpy.subtract() function is used when we want to compute the difference between two numbers or arrays. It returns the difference of numbers.

Program description:- Python program to subtract two numbers using the built-in function

# Python program to subtract two numbers using function

# importing numpy.subtract() function
import numpy

def subtract_num(a,b):   #user-defined function
   subtract = numpy.subtract(num1, num2)   #subtracting numbers
   return subtract   #return value

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

# calling function
subtract = subtract_num(num1, num2)

# print subtraction of numbers
print('The subtraction of numbers {0} and {1} is {2}'
                         .format(num1, num2, subtract))

Output for the input values test-case-1:-

Enter first number: 56
Enter second number: 12
The subtraction of numbers 56.0 and 12.0 is 44.0

Output for the input values test-case-2:-

Enter first number: 25.3
Enter second number: 0.98
The subtraction of numbers 25.3 and 0.98 is 24.32

Subtract Two Numbers in Python using Recursion

This python program also performs the same task but in different ways. In this program, we subtract two numbers without using the (-) operator.

# Python program to subtract two numbers using function

def subtract_num(a,b):   #user-defined function
   if (b == 0):
      return a
   return subtract_num(a ^ b, (~a & b) << 1)

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

# calling function
subtract = subtract_num(num1, num2)

# print subtract of numbers
print('The subtract of numbers {0} and {1} is {2}'
                      .format(num1, num2, subtract))

Output for the input values test-case-1:-

Enter first number: 125
Enter second number: 32
The subtract of numbers 125 and 32 is 93

Leave a Comment

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