Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Print All Factors

Print all positive factors of an positive integer with Python. Students trace an inclusive divisor scan and distinguish factors from multiples and remainders.

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

In this one

  • Define positive factors
  • Include num itself
  • Format a filtered sequence

How it works

  1. 1

    Visit candidates one through num.

  2. 2

    Print a candidate if num % i is zero.

  3. 3

    End the shared line after the search.

Edge cases

Is num itself listed?

Is num itself listed?

Yes. Every positive integer divides itself.

What happens for a negative input?

What happens for a negative input?

This ascending range prints no factors.

What's going on

A factor divides num with remainder zero. The loop tries every candidate from one through num, including the number itself.

For six, the printed candidates are one, two, three, and six. The values share a line because print uses end with a space. This is a direct search rather than an optimized factor-pair method; it makes one divisibility test per candidate.

Is num itself listed? Yes. Every positive integer divides itself.

What happens for a negative input? This ascending range prints no factors.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which values are factors of six?

Pick an answer to check it.

Question 2

Is num itself listed?

Pick an answer to check it.

Questions people ask

Is num itself listed?

Yes. Every positive integer divides itself.

What happens for a negative input?

This ascending range prints no factors.

Which values are factors of six?

1, 2, 3, 6.