text: Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) C # 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 Code
code: using System ; class GFG { static void printDivisors ( int n ) { int [ ] v = new int [ n ] ; int t = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) Console . Write ( i + " ▁ " ) ; else { Console . Write ( i + " ▁ " ) ; v [ t ++ ] = n / i ; } } } for ( int i = t - 1 ; i >= 0 ; i -- ) Console . Write ( v [ i ] + " ▁ " ) ; } public static void Main ( ) { Console . Write ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ STRNEWLINE " ) ; printDivisors ( 100 ) ; } }