In this article, we will show you, How to write a Python Program to find Factors of a Number using While Loop, For Loop, and Functions with example.

**NOTE:** Integers that are completely divisible by a given number (it means remainder = 0) are called as factors.

## Python Program to find Factors of a Number using While Loop

This Python program allows user to enter any integer value. Next, this program will find Factors of that number using While Loop.

# Python Program to find Factors of a Number number = int(input("Please Enter any Number: ")) value = 1 print("Factors of a Given Number {0} are:".format(number)) while (value <= number): if(number % value == 0): print("{0}".format(value)) value = value + 1

**OUTPUT**

**ANALYSIS**

Within the while loop, there is a If statement to check, whether Number divisible by value is exactly equal to 0 or not. If it is True, it will print that value otherwise, it will skip that value and check next value.

number = 4, value = 1

**First Iteration**

(value <= number) – It means (1 <= 4) is True

Now, Check the if condition

if (number % value == 0) => (4 % 1 ==0) – This condition is TRUE. So, 1 is printed

value = value + 1 – means value become 2

**Second Iteration**

value = 2 and Number = 4 – It means (2 <= 4) is True

if(4 % 2 ==0) – This condition is TRUE. So, 2 is printed

**Third Iteration**

value = 3 and Number = 4 – It means (3 <= 4) is True

if(3 % 2 ==0) – Condition is FLASE. So, 3 Will be Skipped

**Fourth Iteration**

i = 4 and Number = 4 – It means (4 <= 4) is True

if(4 % 4 ==0) – Condition is TRUE. 4 is printed

Next, value will become 5 – It means condition (5 <= 4) is Flase. So, loop will be Terminated.

Factors of a given number 4 = 1, 2, 4

## Python Program to find Factors of a Number using For Loop

In this program, we just replaced the While Loop with For Loop.

# Python Program to find Factors of a Number number = int(input("Please Enter any Number: ")) print("Factors of a Given Number {0} are:".format(number)) for value in range(1, number + 1): if(number%value == 0): print("{0}".format(value))

**OUTPUT**

## Python Program to find Factors of a Number using Functions

This program is same as above example, but this time we separated the logic by defining a new function called Find_factors.

# Python Program to find Factors of a Number def Find_Factors(number): for value in range(1, number + 1): if(number % value == 0): print("{0}".format(value)) num = int(input("Please Enter any Number: ")) print("Factors of a Given Number {0} are:".format(num)) Find_Factors(num)

**OUTPUT**