Python Program to Find sum of Even Factors of a Number

In the previous article, we have discussed Python Program to Print Number in Ascending Order which contains 1, 2 and 3 in their Digits.
Given a number, and the task is to get the sum of even 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 even factors of 24 = 2+4+6+ 8+12+24 = 56

Examples:

Example1:

Input:

Given Number = 60

Output:

The Sum of all even factors of { 60 }  =  144

Example2:

Input:

Given Number = 24

Output:

The Sum of all even factors of { 24 }  =  56

Program to Find the Sum of Even Factors of a Number in Python

Below are the ways to the sum of even 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 an empty list and store it in another variable.
  • Loop from ‘1’  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, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even 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 an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, 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, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 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 even 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 even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

The Sum of all even factors of { 24 }  =  56

Method #2: Using For loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Take an empty list and store it in another variable.
  • Loop from ‘1’  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, Check if the iterator modulus 2 is equal to 0 using the if conditional statement.
  • If the statement is True, append the iterator value to the above-declared list.
  • Get the sum of all the even factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the even 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 an empty list and store it in another variable.
all_factors = []
# Loop from '1' to above given number range using For loop.
for itr in range(1, 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, Check if the iterator modulus 2 is equal to 0 using the
# if conditional statement.
        if itr % 2 == 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 even 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 even factors of a given number.
print("The Sum of all even factors of {", gvn_numb, "}  = ", reslt)

Output:

Enter some random Number = 32
The Sum of all even factors of { 32 } = 62

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.