Calculations

Python program to subtract two number using Function

Python program to subtract two number using Function

In this tutorial, we will discuss the Python program to subtract two number using Function

In this topic, we are going to learn how to subtract two numbers (integer, floating point) using the function in Python language

already we are learned the same concept using the operator

if you want to remember click here, Python program to subtract two numbers

Subtract two number using Function

Approach

  • Variable declaration
  • Read value from user
  • function definition
  • call the function
  • display result on the screen

 

Code to subtract two integer number using Function

The program allows to enter two integer values then, it calculates subtraction of the given numbers

Program 1

#python program to subtract two numbers using function

def subtraction(x,y):  #function definifion for subtraction
    sub=x-y
    return sub
num1=int(input("please enter first number: "))#input from user to num1
num2=int(input("please enter second number: "))#input from user to num2

print("Subtraction is: ",subtraction(num1,num2))#call the function

When the above code is executed, it produces the following result

case 1

please enter first number: 100
please enter second number: 46
Subtraction is:  54

 

case 2

please enter first number: 54
please enter second number: 110
Subtraction is: -56

 

Code to subtract two float number using Function

The program allows to enter two float values then, it calculates subtraction of the given numbers

Program 2

#python program to subtract two float numbers using function

def subtraction(x,y):  #function for subtraction
    sub=x-y
    return sub
num1=float(input("please enter first number: "))#input from user to num1
num2=float(input("please enter second number: "))#input from user to num2

print("Sum is: ",subtraction(num1,num2))#call thefunction

When the above code is executed, it produces the following result

case 1

please enter first number: 56.7
please enter second number: 45.6
Subtraction is:  11.1

 

Suggetsed post

Function in Python programming language

Data type in Python language

Operator in Python

 

Similar post

Subtract two numbers in Java language

Subtract two numbers in C language

Subtract two numbers in C++ language

Subtract two numbers in Python language

C++ program to subtract two number using Function
Java program to subtract two number using method
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago