How to find all the factors of a Number in Python?

Estimated read time 2 min read

To find all the factors of a number in Python, you can use a simple loop to iterate over all possible divisors. Here’s an example:

def find_factors(number):
    factors = []
    for i in range(1, number + 1):
        if number % i == 0:
            factors.append(i)
    return factors

# Test the function
number = 36  # Replace with your number
factors = find_factors(number)
print("Factors of", number, "are:", factors)

In this example, we define a function called find_factors that takes a number as an argument. Inside the function, we initialize an empty list called factors to store the factors.

We then iterate over the numbers from 1 to the given number (inclusive) using a loop. For each iteration, we check if the number is divisible by the current iteration value (i.e., if number % i == 0). If it is divisible, we add the current value of i to the factors list.

Finally, we return the factors list.

You can test the function by passing your desired number to the find_factors function, and it will return a list of all the factors of that number. In the example, we use number = 36 to find the factors of 36 and print them.

Note that factors are positive integers that evenly divide the given number. The factors are calculated by checking if the number modulo the current divisor is equal to zero. This ensures that the current divisor is a factor of the number.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply