Python program to find the percentage difference between two numbers

Python example program to find the change in percentage between two numbers :

In this post, I will show you how we can find the percentage difference between two numbers in Python.

This is a beginner-level Python program and you will learn how to take user inputs and how to calculate the difference of two numbers in percentage in Python.

Method 1: Steps we will use in the program:

Following steps we will use in this program:

  • Ask the user to enter the first number
  • Ask the user to enter the second number
  • The second number should be greater than the first number. Because we are finding the change in percentage between the first and the second number. If the second number is smaller than the first number, enter one message that the numbers are invalid.
  • Calculate the percentage difference between the two numbers.
  • Print out the result to the user.

Python program:

Let’s write down the Python program:

first_number = int(input("Enter your first number: "))
second_number = int(input("Enter your second number: "))

if second_number < first_number:
    print("Please enter a valid number")
else:
    percent_diff = ((second_number - first_number) / first_number) * 100

    print("Difference in percentage is: {:.2f}%".format(percent_diff))

Download it on GitHub

python program to find the difference between two numbers in percentage

Explanation:

  • The first number is assigned to the variable first_number and the second number is assigned to the variable second_number.
  • It uses the input() method to read the user input.
  • The if condition checks if the second number is smaller than the first number or not. If yes, it prints a message to the user to enter a valid number. Else, it calculates the percentage difference. The percent difference is assigned to the percent_diff variable.
  • Finally, it is printing the calculated difference value, i.e. percent_diff.

Sample output:

If you run this program, it will print outputs as below:

Enter your first number: 10
Enter your second number: 20
Difference in percentage is: 100.00%

Enter your first number: 100
Enter your second number: 1000
Difference in percentage is: 900.00%

Enter your first number: 23
Enter your second number: 43
Difference in percentage is: 86.96%

Method 2: How to handle negative percentages:

In the previous example, the program works only if the first number is smaller than the second number. We can also write it to work even if the first number is bigger than the second one. The percentage difference will be a negative value. The following program shows how it works:

first_number = int(input("Enter your first number: "))
second_number = int(input("Enter your second number: "))

if second_number < first_number:
    percent_diff = (-1) * ((first_number - second_number) / second_number) * 100
else:
    percent_diff = ((second_number - first_number) / first_number) * 100

print("Difference in percentage is: {:.2f}%".format(percent_diff))

Download it on GitHub

If the second number is smaller than the first number, it calculates the percentage difference and multiplies it with -1 to print the result: (-1) * ((first_number - second_number) / second_number) * 100.

If you run this program, it will print outputs as below:

Enter your first number: 23
Enter your second number: 43
Difference in percentage is: 86.96%

Enter your first number: 43
Enter your second number: 23
Difference in percentage is: -86.96%

You might also like: