Sum of odd divisors – Python Program to Find Sum of Odd Factors of a Number

Sum of odd divisors: 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 sum 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.

Applications you must try:

  1. Python Program For Number Of Elements With Odd Factors In A Given Range?
  2. Python Program For Find Sum Of Odd Factors Of A Number?
  3. Python Program To Find Factors Of A Number?
  4. Find Factors In Python?

Recommended Reading On: Python Program to Find the Sum of Digits of a Number at Even and Odd places

Conclusion

Sum Of All Factors Of A Number in python, Factors of number in python, Sum of Aal factors in python, Python factors of a number, Factors of given number in python, Python get factors of a number, Factor of a number in python, Get factors of a number python are the basic foundation to excel in this concept must have known this.