{"id":18317,"date":"2021-08-26T10:42:06","date_gmt":"2021-08-26T05:12:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=18317"},"modified":"2021-11-22T18:37:15","modified_gmt":"2021-11-22T13:07:15","slug":"python-program-to-find-sum-of-odd-factors-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-odd-factors-of-a-number\/","title":{"rendered":"Python Program to Find Sum of Odd Factors of a Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find sum of Even Factors of a Number<\/a> Factors are numbers or algebraic expressions that divide another number by themselves and leave no remainder.<\/p>\n Example: let the given number = 24<\/p>\n # The factors of 24 are : 1, 2, 3, 4, 6, 8, 12, 24<\/p>\n The sum of odd factors of 24 = 1+3= 4<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to the sum of odd factors of a given number.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a number, and the task is to get the sum of odd factors of a given number.<\/p>\nGiven Number = 24<\/pre>\n
The Sum of all odd factors of { 24 } = 4<\/pre>\n
Given Number = 72<\/pre>\n
The Sum of all odd factors of { 72 } = 13<\/pre>\n
Program to Find the Sum of Odd Factors of a Number in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 24\r\n# Take an empty list and store it in another variable.\r\nall_factors = []\r\n# Loop from '1' to above given number range using For loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Check whether the given number modulus iterator value is equal to '0' or not\r\n # using if conditional statement.\r\n if gvn_numb % itr == 0:\r\n # If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the\r\n # if conditional statement.\r\n if itr % 2 != 0:\r\n # If the statement is True ,append the iterator value to the above declared list .\r\n all_factors.append(itr)\r\n # Get the sum of all the odd factors of above got list using built-in sum() function\r\n # and store it in another variable.\r\nreslt = sum(all_factors)\r\n# Print the sum of all odd factors of a given number.\r\nprint(\"The Sum of all odd factors of {\", gvn_numb, \"} = \", reslt)\r\n<\/pre>\n
The Sum of all odd factors of { 24 } = 4<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number as user input using int(input()) and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random Number = \"))\r\n# Take an empty list and store it in another variable.\r\nall_factors = []\r\n# Loop from '1' to above given number range using For loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Check whether the given number modulus iterator value is equal to '0' or not\r\n # using if conditional statement.\r\n if gvn_numb % itr == 0:\r\n# If the statement is True, Check if the iterator modulus 2 is not equal to 0 using the\r\n# if conditional statement.\r\n if itr % 2 != 0:\r\n # If the statement is True ,append the iterator value to the above declared list .\r\n all_factors.append(itr)\r\n# Get the sum of all the odd factors of above got list using built-in sum() function\r\n# and store it in another variable.\r\nreslt = sum(all_factors)\r\n# Print the sum of all odd factors of a given number.\r\nprint(\"The Sum of all odd factors of {\", gvn_numb, \"} = \", reslt)\r\n<\/pre>\n
Enter some random Number = 56\r\nThe Sum of all odd factors of { 56 } = 8<\/pre>\n
\n