Python program to find factors of a number using for loop and while loop
ADVERTISEMENTS
In this article, you will learn how to find factors of a number using for loop and while loop in the python programming language.
Examples
Input: 60
The factors of the 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
Input: 70
The factors of the 70 are: 1 2 5 7 10 14 35 70
What are the factors of a number?
The factors of a number are defined as numbers that divided the original number without leaving any remainder (left reminder = 0).
You should have knowledge of the following topics in python programming to understand these programs:
- Python
input()
function - Python
int()
function - Python
for
loop statement - Python
while
loop statement - Python
if
statement - Python
print()
function
1. Python program to find factors of a number using for loop
# Python program to find factors of a number using for loop
print ("Enter the positive integer number: ", end="")
random_number = int (input ())
print ("The factors of the ", random_number, "are: ", end="")
for i in range (1, random_number + 1):
if random_number % i == 0:
print (i, end=" ")
print (end="\n")
Output
Enter the positive integer number: 60
The factors of the 60 are: 1 2 3 4 5 6 10 12 15 20 30 60
2. Python program to find factors of a number using while loop
# Python program to find factors of a number using while loop
print ("Enter the positive integer number: ", end="")
random_number, i = int (input ()), 1
print ("The factors of the ", random_number, "are: ", end="")
while i <= random_number:
if random_number % i == 0:
print (i, end=" ")
i += 1
print (end="\n")
Output
Enter the positive integer number: 70
The factors of the 70 are: 1 2 5 7 10 14 35 70
Explanation
In these given programs, we have taken input 60
a random number then applied the for
loop and makes a calculation on this random number.
With It self reminder zero to find the possible factors of this random number.
The same calculation applied to the second program with while
loop.