Python Program to Calculate the Percentage of a Number

Percentage

Python programs to calculate the percentage of a number have been given below. A percentage is represented as a number or a ratio which is a fraction of 100. It is denoted by the symbol '%'. For example 50% represents (50 / 100) or 0.50. So, 50% of a number b will be calculated as b * (50 / 100) or b * 0.50.


Suppose a and b are two numbers. Then, the solution to the percentage problem is obtained by answering any of the following queries.

(i) What is a% of b?

(ii) a is what % of b?

(iii) a is b% of what?


Let's assume a = 15 and b = 60, then the solutions w.r.t the above queries:

(i) ___ is a% of b := 15% of 60 := 60 * (15 / 100) := 9

(ii) a is ___ % of b := (15 / 60) * 100 := 25%

(iii) a is b% of ___ := (15 / 60) * 100 := 25


In the following section, Python programs to solve the above problems are given along with the algorithm, pseudocode and time-complexity of the programs.





1. Algorithm to calculate percentage of a number


1. Take a number a as input.

2. Take another number b as input where b% of a is to be calculated.

3. Calculate a * (b / 100) and declare it as the output.




2. Pseudocode to calculate percentage of a number


Input : Two numbers a, b

Output : b% of a

1. Procedure percentage(a, b):

2. x := a * (b / 100)

3. Return x

4. End Procedure





3. Time complexity to calculate percentage of a number


Time Complexity: O(1)




4. Programs to calculate percentage




4.1. Python Program to calculate "What is a% of b?"

Code has been copied
# *********************************************
#             alphabetacoder.com
# Python program to calculate what is a% of b?
# *********************************************

# take input
print("Enter values to calculate a% of b")
a = float(input("Enter a = "))
b = float(input("Enter b = "))

# calculate value
x = b * (a / 100)

# display result upto 3 decimal places
print(round(x, 3), "is", round(a, 3), "% of", round(b, 3))

Output


Enter values to calculate a% of b

Enter a = 30

Enter b = 70

21.0 is 30.0 % of 70.0





4.2. Python Program to calculate "a is what % of b?"

Code has been copied
# *********************************************
#             alphabetacoder.com
# Python program to calculate a is what % of b?
# *********************************************

# take input
print("Enter values to calculate a is what % of b?")
a = float(input("Enter a = "))
b = float(input("Enter b = "))

# calculate value
x = (a / b) * 100

# display result upto 3 decimal places
print(round(a, 3), "is", round(x, 3), "% of", round(b, 3))

Output


Enter values to calculate a is what % of b?

Enter a = 120

Enter b = 240

120.0 is 50.0 % of 240.0





4.3. Python Program to calculate "a is b% of what?"

Code has been copied
# *********************************************
#             alphabetacoder.com
# Python program to calculate a is b% of what?
# *********************************************

# take input
print("Enter values to calculate a is b %% of what?")
a = float(input("Enter a = "))
b = float(input("Enter b = "))

# calculate value
x = (a / b) * 100

# display result upto 3 decimal places
print(round(a, 3), "is", round(b, 3), "% of", round(x, 3))

Output


Enter values to calculate a is b % of what?

Enter a = 27

Enter b = 81

27 is 81% of 33.333