codescracker


python

Python Program to Find Factors of Number



« Previous Program Next Program »


Find Factors of Number in Python

To find factors of any number in python, you have to ask from user to enter a number to find and print all the factors of that number on the output screen as shown in the program given here.

Python Programming Code to Find Factors of Number

Following python program ask from user to enter a number to find factors of that number:

# Python Program - Find Factors of Number

print("Enter 'x' for exit.");
num = input("Enter a number: ");
if num == 'x':
    exit();
else:
    count = 0;
    number = int(num);
    for i in range(2, number-1):
    	if number%i == 0:
    	    print(i);
    	    i += 1;
    	    count += 1;
    if count==0:
        print(number,"is a prime number.");

Here is the sample run of the above Python program shows how to find factors of any number:

find factors of number python

Above is the initial output asking from the user to enter a number to find all the factors of that numbers, that is all those numbers that can divide the given number without leaving any remainder. For example, if you enter 10 as input, then you will see 2, 5 as output, as 2 and 5 can divide 10 without leaving any remainder.

And in case if you will provide any number say 7, therefore as you all known 7 is a prime number, it means no any number without 1 and itself the same number that is 7 can divide it that is 7 without leaving any remainder. Therefore in that case, you will see that the number is a prime number as output.

Now let's first check for the factors of a number by applying or providing a composite number as input say 100:

factors of number python

Now let's re-run the same program and check for any prime number say 7.

print factors of number python

Here is the same program on python shell:

python find factors of number

Program Video with Compile and Run


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test