Python program to check if a number is abundant/excessive or not

Python program to check if a number is abundant/ excessive number or not :

In this tutorial, we will learn how to find if a number is abundant/excessive number or not using python. A number is called a abundant number or excessive number if the sum of all of its proper divisors is greater than the number itself. For example, 12 is a abundant number. Because its proper divisors are 1,2,3,4 and 6. Total is 1+2+3+4+6 = 16 which is greater than 12.Similarly, 54, 88, 100,102,112 etc. all are abundant number.
In our program, the user will enter one number and the program will check if it is abundant or not and it will print out the result.

Python program :

#1
input_no = int(input("Enter a number : "))
total = 0

#2
is_abundant = 0

#3
for i in range(1,input_no):
	#4
	if(input_no % i == 0):
		#5
		total = total + i 
		if(total > input_no):
			is_abundant = 1
			break

#6
if((total > input_no) or (is_abundant ==1)):
	print("It is an abundant number.")
else :
	print("It is not an abundant number.")

Explanation :

The commented numbers in the above program denote the step-number below :

1. Ask the user to enter a number and save it in input_no variable. Also initialize one number total to store the sum of all proper divisors of the number.
2. is_abundant is a flag to detect if the number is abundant or not. 1 means it is a abundant number. 0 means not.
3. Start one for loop to run from 1 to input_no -1 .
4. Check for each number, if it is a proper divisor or not. no%i returns the remainder of no/i. So, we are checking if the remainder is 0 or not. If yes, it is a proper divisor.
5. If it is a proper divisor, add this number to the total sum variable total. Also check if the total is more than the user-input no. or not. If yes, it is a abundant number. So, we don’t need to check any further. Set the value to is_abundant as 1 and break from the loop.
6. Finally check if the total sum total is more than input_no or if is_abundant is equal to 1 or not. We are checking both cases because if the for loop runs for all numbers, is_abundant will be 0. Else, if we found the total more than input_no in middle of the loop, is_abundant will be 1. Print out the message to the user if it is an abundant number or not.

Sample Output :

Enter a number : 108
It is an abundant number.

Enter a number : 12
It is an abundant number.

Enter a number : 13
It is not an abundant number.

Enter a number : 14
It is not an abundant number.

Enter a number : 5
It is not an abundant number.

Leave a Reply