Skip to content
thesarfo

Reference

Printing All Divisors

Finding all numbers that evenly divide a given number by looping from 1 to n.

views 0

Given a number, say 36, print the numbers that divide it completely (no remainder) — its divisors: 1, 2, 3, 4, 6, 9, 12, 18, 36.

All possible divisors of a number n fall between 1 and n, so we can loop from 1 to n and check if i divides n evenly (n % i == 0).

static void printDivisors(int n){
for (int i = 1; i <= n; i++){
if (n % i == 0){
System.out.println(i);
}
}
}