Similar to binary conversion
static int ternary ( int number ) { int ans = 0 , rem = 1 , b = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * b ; number = ( int ) ( number / 3 ) ; b = b * 10 ; } return ans ; }