Python Program to Find Factors of Numbers

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)

OUTPUT:
factors

Click here to download the source code.

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Python and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s