Print All Factors

Print all factors of a number.

Logic BuildingIntermediate
Logic Building
# Take number
num = int(input("Enter a number: "))

# Print factors
print(f"Factors of {num}:")
for i in range(1, num + 1):
    if num % i == 0:
        print(i, end=" ")
print()

Output

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

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