I’m writing this post after a longggg gap of about 9th months. Today, we shall learn how to write a python program to find factors of given number. For this, we need to take a number as input to the program. The input() function is used to get the number as a scanf() statement which we use in C language.Since we are using input() function the input will be treated as string. Therefore, by using int() function we shall convert the string to int datatype.
int(input("Enter a number:"))
Now, we shall create a function and then try to find the factors of the given number. For, defining a function we need to use the keyword def and then write the name of the defining function and end the line with :. And then we can continue writing body of our function
def finding(var) :
Finding factors of a given number is easy. From 1 to n+1 we need to check which number divides the given number with zero remainder. For finding the remainder we use % operator.
# definig the function. def finding(x): for i in range(1, x+1): if x % i == 0: print(i); #Input from the user num = int(input("\nEnter a number:")) print("\nFactors of given number are:") finding(num)