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