Find factors of a number in python using for loop; In this tutorial, you will learn how to find factors of a given number in python using for loop.
“Factors” are the numbers you multiply to get another number. For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4.
Python Program to Find the Factors of a Number using For Loop
Follow the below steps and write a program find factors of a number in python using for loop:
- Step 1 – Take input number from user
- Step 2 – Iterate For Loop over every number from 1 to the given number
- Step 3 – If the loop iterator evenly divides the provided number i.e.
number % i == 0
print it. - Step 4 – Print Result
num = int(input("Enter a number: ")) print("The factors of {} are,".format(num)) for i in range(1,num+1): if num % i == 0: print(i)
After executing the python program, the output will be:
Enter a number: 10 The factors of 10 are, 1 2 5 10
Python Program to Find the Factors of a Number by using Functions
Follow the below steps and write a program find factors of a number in python by using functions:
- Step 1 – Create Function, which is calculate factors of number
- Step 2 – Take input number from user
- Step 3 – Call Function
- Step 4 – Print Result
# Python Program to find the factors of a number # This function computes the factor of the argument passed def print_factors(x): print("The factors of",x,"are:") for i in range(1, x + 1): if x % i == 0: print(i) num = int(input("Enter a number: ")) print_factors(num)
After executing the python program, the output will be:
Enter a number: 10 The factors of 10 are, 1 2 5 10