Program to Find Sum of Odd Factors of a Number

Python Program to Find Sum of Odd Factors of a Number

In the previous article, we have discussed Python Program to Find sum of Even Factors of a Number
Given a number, and the task is to get the sum of odd 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 odd factors of 24 = 1+3= 4

Examples:

Example1:

Input:

Given Number = 24

Output:

The Sum of all odd factors of { 24 }  =  4

Example2:

Input:

Given Number = 72

Output:

The Sum of all odd factors of { 72 }  =  13

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

Below are the ways to the sum of odd 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 the if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is not 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 odd factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the odd 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 not 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 odd 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 odd factors of a given number.
print("The Sum of all odd factors of {", gvn_numb, "}  = ", reslt)

Output:

The Sum of all odd factors of { 24 }  =  4

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 the if conditional statement.
  • If the statement is True, Check if the iterator modulus 2 is not 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 odd factors of the above-given list using the built-in sum() function and store it in another variable.
  • Print the sum of all the odd 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 not 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 odd 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 odd factors of a given number.
print("The Sum of all odd factors of {", gvn_numb, "}  = ", reslt)

Output:

Enter some random Number = 56
The Sum of all odd factors of { 56 } = 8

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.