DEV Community

Bala Murugan
Bala Murugan

Posted on

Divisors of given number

Definition

A divisor (or factor) is a number that divides another number exactly without leaving any remainder.

For example, the divisors of 12 are:
1, 2, 3, 4, 6, 12

Because each of these numbers divides 12 completely.

Algorithm

Start

Read the number n

Set i = 1

Check whether i <= n

If true, check n % i == 0

If true, print i

Increment i by 1

Repeat steps 4 to 7 until i > n

Stop

Flowchart:

┌─────────┐
│ Start │
└────┬────┘


┌──────────────┐
│ Input n │
└────┬─────────┘


┌──────────────┐
│ i = 1 │
└────┬─────────┘


┌──────────────┐
│ i <= n ? │◄─────────┐
└────┬─────┬───┘ │
│Yes │No │
▼ ▼ │
┌─────────┐ ┌─────────┐ │
│n%i==0 ? │ │ Stop │ │
└──┬───┬──┘ └─────────┘ │
│Yes│No │
▼ │ │
┌───────┐ │
│Print i│ │
└───┬───┘ │
│ │
▼ │
┌─────────┐ │
│ i=i+1 │────────────────┘
└─────────┘

Code:

n = int(input("Enter a number: "))
i = 1

while i <= n:
    if n % i == 0:
        print(i)
    i += 1
Enter fullscreen mode Exit fullscreen mode

Example:

Input:

12
Enter fullscreen mode Exit fullscreen mode
Output:

1
2
3
4
6
12
Enter fullscreen mode Exit fullscreen mode

Top comments (0)