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 important 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
Table of Contents
What Are Factors?
In math, a factor is a number that divides another number evenly, that is, with no remainder. Factors can be algebraic expressions as well, dividing another expression evenly.
81÷9 = 9
Hence, 9 divides 81 into 9 equal parts.
9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 81
A factor is a number that divides the given number without any remainder. Factors of a number can be referred to as numbers or algebraic expressions that evenly divide a given number/expression. The factors of a number can either be positive or negative.
For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.
Video Explanation of Factors of a number in Python
Print Factors of a number in Python
Properties of Factors
Factors of a number have a certain number of properties. Given below are the properties of factors:
- The number of factors of a number is finite.
- A factor of a number is always less than or equal to the given number.
- Every number except 0 and 1 has at least two factors, 1 and itself.
- Division and multiplication are the operations that are used in finding the factors.
How to Find Factors of a Number?
We can find factors by both division and multiplication methods
Factors by Division
To find the factors of a number using division:
- Find all the numbers less than or equal to the given number.
- Divide the given number by each of the numbers.
- The divisors that give the remainder to be 0 are the factors of the number
Factors by Multiplication
To find the factors using the multiplication:
- Write the given number as the product of two numbers in different possible ways.
- All the numbers that are involved in all these products are the factors of the given number.
Let’s move to our code.
Method 1: Factors of a number using for loop in Python
n = int(input("Enter any number:")) for i in range(1, n+1): if n % i == 0: print(i)
Enter any number:10 1 2 5 10
Code Explanation Method 1: Factors of a number using for loop in Python
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, 10.
Method 2: Factors of a number using while loop in Python
num = int(input("Enter a number:")) i = 1 while i <= num : if (num % i==0) : print("The factor of",num ,"are:",i) i = i + 1
Enter a number:15 The factor of 15 are: 1 The factor of 15 are: 3 The factor of 15 are: 5 The factor of 15 are: 15
Code Explanation Method 2: Factors of a number using while loop in Python
Let’s understand this code now. In this program, we will use a while loop. Here we are taking user input for a number to get factorial and convert the same number into an int, and we will store this into a variable num.
Now in the next line, define variable i and assign value 1; we use it as a counter. In the next line, we have a while loop, and this loop will execute till i is less than or equal to num. Inside the while block, we will check if num is divisible by i which is the counter.
If num is divisible by i, then i is a factor of num and we will print the i with a message. Outside of the condition, we will write our increment of the counter. Let’s run this program and enter value 15; you will get the below output.
The factor of 15 are: 1
The factor of 15 are: 3
The factor of 15 are: 5
The factor of 15 are: 15
I know you are getting this topic with enough understanding of factors; I am sure you can crack any interview or solve problems on factors in just seconds. Happy learning to you!