In the previous article, we have discussed Python Program to Find Product of Values of elements in a Dictionary
Given a number, and the task is to get the sum of all the factors of a given number.
Factors are numbers or algebraic expressions that divide another number by themselves and leave no remainder.
Example: let the given number = 24
# The factors of 24 are : 1, 2, 3, 4, 6, 8, 12, 24
The sum of all the factors of 24 = 1+2+ 3+4+6+ 8+12+24 = 60
Examples:
Example1:
Input:
Given Number = 24
Output:
The Sum of all the factors of { 24 } is = 60Example 2:
Input:
Given Number = 140
Output:
The Sum of all the factors of { 140 } is = 336Program to Get Sum of all the Factors of a Number
Below are the ways to get the sum of all the factors of a given number.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a list and initialize it with ‘1’ and store it in another variable.
- Loop from ‘2’ to the above-given number range using For loop.
- Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
- If the statement is True, append the iterator value to the above-declared list.
- Get the sum of all the factors of the above-given list using the built-in sum() function and store it in another variable.
- Print the sum of all the factors of a given 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 list and initialize it with '1' and store it in another variable.
all_factors = [1]
# Loop from '2' to above given number range using For loop.
for itr in range(2, gvn_numb+1):
# Check whether the given number modulus iterator value is equal to '0' or not
# using if conditional statement.
if gvn_numb % itr == 0:
# If the statement is True ,append the iterator value to the above declared list .
all_factors.append(itr)
# Get the sum of all the factors of above got list using built-in sum() function
# and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all the factors of a given number.
print("The Sum of all the factors of {", gvn_numb, "} is = ", reslt)
Output:
The Sum of all the factors of { 24 } is = 60Method #2: Using For Loop (User Input)
Approach:
- Give the number as user input using int(input()) and store it in a variable.
- Take a list and initialize it with ‘1’ and store it in another variable.
- Loop from ‘2’ to the above-given number range using For loop.
- Check whether the given number modulus iterator value is equal to ‘0’ or not using if conditional statement.
- If the statement is True, append the iterator value to the above-declared list.
- Get the sum of all the factors of the above-given list using the built-in sum() function and store it in another variable.
- Print the sum of all the factors of a given number.
- The Exit of the program.
Below is the implementation:
# Give the number as user input using int(input()) and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Take a list and initialize it with '1' and store it in another variable.
all_factors = [1]
# Loop from '2' to above given number range using For loop.
for itr in range(2, gvn_numb+1):
# Check whether the given number modulus iterator value is equal to '0' or not
# using if conditional statement.
if gvn_numb % itr == 0:
# If the statement is True ,append the iterator value to the above declared list .
all_factors.append(itr)
# Get the sum of all the factors of above got list using built-in sum() function
# and store it in another variable.
reslt = sum(all_factors)
# Print the sum of all the factors of a given number.
print("The Sum of all the factors of {", gvn_numb, "} is = ", reslt)
Output:
Enter some random number = 140
The Sum of all the factors of { 140 } is = 336Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
