Given a number as an input to your Python program, you have to find all the factors of that given number. factors of a number which can divide the number completely is known as factor of a number.
For example let's take number 9
If we try to divide the number 9 by 3 the reminder will be zero. 3 is a factor of 9
Let's take another example 11
If we try to divide the number 11 by 3 the reminder will be 2, three is not a factor of 11
Write a Python Function That Print All Factors of a Given Number
Let's define a function get_factors() and pass the input argument that is there integer number to it. using the for loop iterate from 1 to to the value of n + 1 that is number + 1 and get the index value. try dividing the number by the index if the the reminder is zero then print the index as factor of a number
def get_factors(n):
for index in range(1,n+1):
if n % index == 0:
print(index)
In the main program lets take a variable number and ask a user to enter a number using the input function call and then convert it to an integer.
print a user friendly message that is all factors of a number and call the function by providing the input argument as a user entered number.
number = int(input("Enter a Number : "))
print(f"All Factors of Number {number} are ...")
get_factors(number)
Python code
====================
def get_factors(n):
for index in range(1,n+1):
if n % index == 0:
print(index)
number = int(input("Enter a Number : "))
print(f"All Factors of Number {number} are ...")
get_factors(number)
OUTPUT
===============
Enter a Number : 33
All Factors of Number 33 are ...
1
3
11
33
Output 2
===============
Enter a Number : 10000
All Factors of Number 10000 are ...
1
2
4
Tu5
8
10
16
20
25
40
50
80
100
125
200
250
400
500
625
1000
1250
2000
2500
5000
10000
Conclusion
================
Execute the above program by dividing the Random numbers and right oh no doubt the results try dividing the factors by the number by yourself using a simple calculator and check the reminder.
Comment down below if you have any queries or suggestions to improve above program
0 Comments