Python Program to Find the Factors of a Number

 

Python Program to Find the Factors of a Number

 

# Python Program to find the factors of a number

# This function computes the factor of the argument passed
l=[]
# To store values list is used

def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
l.append((i,x/i))
return l

num = int(input("Enter the number: "))
print(print_factors(num))
 
Output:
Enter the number: 320
The factors of 320 are:
[(1, 320.0), (2, 160.0), (4, 80.0), (5, 64.0), (8, 40.0), (10, 32.0), (16, 20.0), (20, 16.0), (32, 10.0), (40, 8.0), (64, 5.0), (80, 4.0), (160, 2.0), (320, 1.0)] 

Note: To find the factors of another number, change the value of num.

In this program, the number whose factor is to be found is stored in num, which is passed to the print_factors() function. This value is assigned to the variable x in print_factors().

In the function, we use the for loop to iterate from i equal to x. If x is perfectly divisible by i, it's a factor of x.

 

 

Comments

Popular posts from this blog

SAME NAME IN CLASS METHOD AND CONSTRUCTOR

PALINDROME NUMBER

FIBONACCI SERIES