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.- def evenfactorssum(n):
- s = 0
- for i in range(1, n+1):
- if n % i == 0:
- if i % 2 == 0:
- s = s+i
- return s
- while True:
- print("\n================= Find Sum of Even Factors Numbers =================\n\n")
- n = int(input("Enter a number: "))
- print("Result: ", evenfactorssum(n))
- opt = input("\nDo you want to try again?(yes/no): ")
- if opt.lower() == 'yes':
- ret=False
- elif opt.lower() == 'no':
- ret=True
- print("Exiting program....")
- else:
- print("Please enter yes/no:")
- break
- if ret == False:
- 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
Add new comment
- 40 views