{"id":16650,"date":"2021-08-12T09:25:13","date_gmt":"2021-08-12T03:55:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=16650"},"modified":"2021-11-22T18:38:32","modified_gmt":"2021-11-22T13:08:32","slug":"python-program-to-get-sum-of-all-the-factors-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-get-sum-of-all-the-factors-of-a-number\/","title":{"rendered":"Python Program to Get Sum of all the Factors of a Number"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Product of Values of elements in a Dictionary<\/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 all the factors of 24 = 1+2+ 3+4+6+ 8+12+24 = 60<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get the sum of all the 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 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: …<\/p>\n
\nGiven a number, and the task is to get the sum of all the factors of a given number.<\/p>\nGiven Number = 24<\/pre>\n
The Sum of all the factors of { 24 } is = 60<\/pre>\n
Given Number = 140<\/pre>\n
The Sum of all the factors of { 140 } is = 336<\/pre>\n
Program to Get Sum of all the Factors of a Number<\/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 a list and initialize it with '1' and store it in another variable.\r\nall_factors = [1]\r\n# Loop from '2'\u00a0to above given number\u00a0range using For loop.\r\nfor itr in range(2, 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 ,append the iterator value to the above declared list .\r\n all_factors.append(itr)\r\n # Get the sum of all the 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 the factors of a given number.\r\nprint(\"The Sum of all the factors of {\", gvn_numb, \"} is = \", reslt)\r\n<\/pre>\n
The Sum of all the factors of { 24 } is = 60<\/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 a list and initialize it with '1' and store it in another variable.\r\nall_factors = [1]\r\n# Loop from '2'\u00a0to above given number\u00a0range using For loop.\r\nfor itr in range(2, 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 ,append the iterator value to the above declared list .\r\n all_factors.append(itr)\r\n # Get the sum of all the 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 the factors of a given number.\r\nprint(\"The Sum of all the factors of {\", gvn_numb, \"} is = \", reslt)\r\n<\/pre>\n
Enter some random number = 140\r\nThe Sum of all the factors of { 140 } is = 336<\/pre>\n
\n