Table of Contents

Positive Difference of Two Numbers | Pictoblox

Example Description
We take two numbers as input from user and find their positive difference - absolute value of subtraction between larger and smaller number

Introduction:

In this Python program, we are going to find the positive difference between two numbers. We will take these two numbers as input from the user and calculate their positive difference using an if-else condition. The positive difference is the absolute value of the subtraction between the larger number and the smaller number.

Code:

#Program to print the positive difference of two numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
 diff = num1 - num2
else:
 diff = num2 - num1
print("The difference of",num1,"and",num2,"is",diff)

Logic:

  1. Take the first number as input from the user and convert it to an integer.
  2. Take the second number as input from the user and convert it to an integer.
  3. Use an if-else condition to check if the first number is greater than the second number.
  4. If it is, subtract the second number from the first number and assign the result to the variable “diff”.
  5. If it is not, subtract the first number from the second number and assign the result to the variable “diff”.
  6. Print the message “The difference of [num1] and [num2] is [diff]” to display the positive difference between the two numbers.

Output:

Enter first number: 19

Enter second number: 17

>> The difference of 19 and 17 is 2

 

Enter first number: 17

Enter second number: 19

>> The difference of 17 and 19 is 2