How to Find Sum of Even Factors Numbers in Python

In this tutorial, we’ll learn how to program "How to Find the Sum of Even Factor Numbers in Python." The objective is to find the sum of even factors of a number based on the given input. A sample program will be provided to demonstrate the process of identifying and summing the even factors. So, let’s get started!

This topic is simple to understand. Just follow the instructions I provide, and you’ll be able to complete it with ease. The program I’ll demonstrate will show you the proper way to find the sum of the even factors of a number. I’ll also include a straightforward and efficient method to achieve this effectively. So, let’s start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def evenfactorssum(n):
  2.         s = 0
  3.         for i in range(1, n+1):
  4.                
  5.                 if n % i == 0:
  6.                        
  7.                         if i % 2 == 0:
  8.                                
  9.                                 s = s+i
  10.                
  11.         return s
  12.  
  13.  
  14. while True:
  15.         print("\n================= Find Sum of Even Factors Numbers =================\n\n")
  16.  
  17.  
  18.         n = int(input("Enter a number: "))
  19.        
  20.         print("Result: ", evenfactorssum(n))
  21.  
  22.         opt = input("\nDo you want to try again?(yes/no): ")
  23.  
  24.         if opt.lower() == 'yes':
  25.                 ret=False
  26.         elif opt.lower() == 'no':
  27.                 ret=True
  28.                 print("Exiting program....")
  29.         else:
  30.                 print("Please enter yes/no:")
  31.                 break
  32.  
  33.         if ret == False:
  34.                 continue

This program calculates the sum of all even factors of a given number \( n \). It does this by iterating through all integers from \( 1 \) to \( n \), checking if they are factors of \( n \) (i.e., \( n \% i == 0 \)), and further verifying if the factor is even (\( i \% 2 == 0 \)). If both conditions are satisfied, the factor is added to a running total. The program repeatedly asks the user if they want to try again until the user responds with "no" to exit.

Output:

There you have it we successfully created How to Find Sum of Even Factors Numbers in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment