text: Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) java program that prints all divisors in sorted order ; method to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver method
code: import java . util . Vector ; class Test { static void printDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) System . out . printf ( " % d ▁ " , i ) ; else { System . out . printf ( " % d ▁ " , i ) ; v . add ( n / i ) ; } } } for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) System . out . printf ( " % d ▁ " , v . get ( i ) ) ; } public static void main ( String args [ ] ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; } }