Find factors of a number in python; Through this tutorial, i am going to show you how to find factors of a given number in python using for loop, while loop and function.
Factor, in mathematics, a number or algebraic expression that divides another number or expression evenly—i.e., with no remainder. For example, 3 and 6 are factors of 12 because 12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other factors of 12 are 1, 2, 4, and 12.
Python Program to Find the Factors of a Number
See the following python programs to python program to find factors of a number using for loop, while loop and function; as shown below:
- Python Program to Find the Factors of a Number using For Loop
- Python Program to Find the Factors of a Number using While Loop
- Python Program to Find the Factors of a Number using Functions
Python Program to Find the Factors of a Number using For Loop
- Step 1 – Get input a number from user in program
- 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 factors of a number
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 using While Loop
- Step 1 – Get input a number from user in program
- Step 2 – Iterate while 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 factors of a number
# Python Program to find Factors of a Number number = int(input("Please Enter any Number: ")) value = 1 print("Factors of a Given Number {0} are:".format(number)) while (value <= number): if(number % value == 0): print("{0}".format(value)) value = value + 1
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 using Functions
- Step 1 – Create Function, which is calculate factors of number
- Step 2 – Get input a number from user in program
- Step 3 – Call Function with a number
- Step 4 – Print factors of a number
# 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