Print All Factors

Print all factors of a number.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 6 of 13
print-all-factors.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Print factors
5print(f"Factors of {num}:")
6for i in range(1, num + 1):
7 if num % i == 0:
8 print(i, end=" ")
9print()

Output

Enter a number: 12
Factors of 12:
1 2 3 4 6 12

What's going on

Check each number from 1 to num.

Key Concepts:

Loop from 1 to num
Check if num % i == 0
If yes, i is a factor