Factors of a Number in Python Using for Loop

Factors of a Number in Python Using for Loop
(Last Updated On: 13/09/2023)

A factor of a number in math is a number that divides the given number. Hence, a factor is nothing but a divisor of the given number. Each prime number will have only two factors, i.e. 1 and the number itself, whereas each composite number will have more than two factors that include prime factors also. Examples are:

In the programming language, the loop condition is considered necessary for printing patterns. In this article, you will learn Alphabet pattern programs using Python.

  • Factors of 2 = 1 and 2
  • Factors of 10 = 1,2,5 and 10

If you want to see all the practice examples and Explanations of Python, then please use this reference to this URL.

Also, See – the complete Python Practice Series.

Program to Print Factors of a Number in Python Using for Loop

n = int(input("Enter any number:"))
for i in range(1, n+1):
    if n % i == 0:
Enter any number:10

Code Explanation: Factors of a Number in Python Using for Loop

In this program, at the very first line, we have accepted a number from the user and converted it into an int, and stored the same number into variable n. In the next line, we have initiated for loop to check factors of numbers.

Here we have for loop having a counter as i and then this loop will start from 1 to the number the user has provided to check the factor plus 1. Then we have an if condition to check if the counter variable number can fully divide the input number or not. If yes, then we will print the counter number as a factor of the number.

Let’s run this program and enter the number 10; the program will return its factors 1, 2, 5, and 10.

Video Explanation of Factors of a number in Python