In the previous article, we have discussed Python Program to Reverse the Order of Palindrome Words in a Sentence
Abundant Number:
An abundant number is one in which the sum of the number’s proper divisors is greater than the number itself.
To check for an Abundant number, find and add the number’s proper divisors, then compare the sum to the number; if the sum is greater than the number, the number is an Abundant number; otherwise, the number is not an Abundant number. The difference between the sum and the number is referred to as abundant.
For example :
Let Number = 18
The proper divisors are:
1*18
6*3
9*2
The sum=1+6+9+3+2= 21 >18
Therefore 18 is an Abundant Number.
Some of the examples of Abundant Numbers are:
12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66…..and so on.
Examples:
Example1:
Input:
Given Number = 24
Output:
The given Number { 24 } is an Abundant Number
Example2:
Input:
Given Number = 25
Output:
The given Number { 25 } is Not an Abundant Number
Program to Check Abundant Number or Not in Python
Below are the ways to check whether the given number is an Abundant Number or not in Python
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a variable say “divisrs_sum” and initialize it with 1.
- Iterate from 2 to the given number using the for loop.
- Check if the given number modulus iterator value is equal to 0 or not using the if conditional statement.
- If the statement is true, then add the iterator with the sum of divisors “divisrs_sum” and store it in the same variable “divisrs_sum”.
- Check if the sum of divisors (divisrs_sum) is greater than the given number using the if conditional statement.
- If the statement is true, then print “The given Number is an Abundant Number”.
- If it is false, then print “The given Number is Not an Abundant Number”.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numb = 24 # Take a variable say "divisrs_sum" and initialize it with 1. divisrs_sum = 1 # Iterate from 2 to the given number using the for loop. for itr in range(2, gvn_numb): # Check if the given number modulus iterator value is equal to 0 or not using the # if conditional statement. if(gvn_numb % itr == 0): # If the statement is true, then add the iterator with the sum of divisors "divisrs_sum" # and store it in the same variable "divisrs_sum". divisrs_sum = divisrs_sum + itr # Check if the sum of divisors (divisrs_sum) is greater than the given number using the # if conditional statement. if(divisrs_sum > gvn_numb): # If the statement is true, then print "The given Number is an Abundant Number". print("The given Number {", gvn_numb, "} is an Abundant Number") else: # If it is false, then print "The given Number is Not an Abundant Number". print("The given Number {", gvn_numb, "} is Not an Abundant Number")
Output:
The given Number { 24 } is an Abundant Number
Method #2: Using For loop (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Take a variable say “divisrs_sum” and initialize it with 1.
- Iterate from 2 to the given number using the for loop.
- Check if the given number modulus iterator value is equal to 0 or not using the if conditional statement.
- If the statement is true, then add the iterator with the sum of divisors “divisrs_sum” and store it in the same variable “divisrs_sum”.
- Check if the sum of divisors (divisrs_sum) is greater than the given number using the if conditional statement.
- If the statement is true, then print “The given Number is an Abundant Number”.
- If it is false, then print “The given Number is Not an Abundant Number”.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function and # store it in a variable. gvn_numb = int(input("Enter some random Number = ")) # Take a variable say "divisrs_sum" and initialize it with 1. divisrs_sum = 1 # Iterate from 2 to the given number using the for loop. for itr in range(2, gvn_numb): # Check if the given number modulus iterator value is equal to 0 or not using the # if conditional statement. if(gvn_numb % itr == 0): # If the statement is true, then add the iterator with the sum of divisors "divisrs_sum" # and store it in the same variable "divisrs_sum". divisrs_sum = divisrs_sum + itr # Check if the sum of divisors (divisrs_sum) is greater than the given number using the # if conditional statement. if(divisrs_sum > gvn_numb): # If the statement is true, then print "The given Number is an Abundant Number". print("The given Number {", gvn_numb, "} is an Abundant Number") else: # If it is false, then print "The given Number is Not an Abundant Number". print("The given Number {", gvn_numb, "} is Not an Abundant Number")
Output:
Enter some random Number = 48 The given Number { 48 } is an Abundant Number
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.