Python

Python program to subtraction of two numbers | 4 different Methods

Python program to subtraction of two numbers| 4 different Methods

In this tutorial, we will discuss the Python program to subtraction of two numbers

In this post, we are going to learn how to find subtraction of two numbers via different 4 ways in Python language

 

Subtraction of two numbers

Subtraction of two numbers – standard method

Program 1

num1=432 #variable declaration
num2=234

sub=num1-num2 #calculation
print('Subtraction of {0} and {1} is: {2}'.format(num1,num2,sub))
#display the result

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

Subtraction of 432 and 234 is: 198

 

Approach

  • Integer variable num1 and num2 both are declared and initialized
  • the num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • finally the program is displayed the output using print() function

 

Subtraction of two numbers – using user input

Program 2

#Subtraction of two numbers - using user input
num1=input("Enter first number ")
num2=input("Enter second number ")
#get input from the user

sub=num1-num2
#calculate subtraction

print('Subtraction of {0} and {1} is: {2}'.format(num1,num2,sub))
#display the result

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

Enter first number 2345
Enter second number 543
Subtraction of 2345 and 543 is: 1802

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input and store the variables in num1, num2.
  • the num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • finally the program is displayed the output using print()

 

Subtraction of two numbers – using user defined function

Program 3

#Python program to subtract two numbers using function
def subtract(a,b): #function definition
    sub=a-b
    return sub
num1=int(input("Please enter the first number "))#input number to num1 from user
num2=int(input("Please enter the second number "))#input number to num2 from user
result=subtract(num1,num2)#calling the function
print('Subtraction of the given numbers',result)#display the output

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

Please enter the first number 4321
Please enter the second number 1234
('Subtraction of the given numbers 3087)

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input  store the variables in num1, num2.
  • define the function for subtract two numbers
  • When we call the function, the num1 and num2 both are subtracted together
  • Then the value is assigned to the integer variable result
  • finally the program is displayed the output using print() function

 

Suggested post

Function in Python language

Operator in Python language

Data type in Python

 

 

Similar post

C program to subtraction of two numbers | 6 different Methods

C++ program to subtraction of two numbers | 6 different Methods

Java program to subtraction of two numbers | 5 different Methods

 

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.