Decimal to octal conversion with minimum use of arithmetic operators | Java implementation of decimal to octal conversion with minimum use of arithmetic operators ; function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Function to find octal equivalent of binary ; add min 0 's in the beginning to make  string length divisible by 3 ; create map between binary and its equivalent octal code ; one by one extract from left , substring of size 3 and add its octal code ; required octal number ; function to find octal equivalent of decimal ; convert decimal to binary ; convert binary to octal required octal equivalent of decimal ; Driver Code
import java . util . * ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } static String convertBinToOct ( String bin ) { int l = bin . length ( ) ; for ( int i = 1 ; i <= ( 3 - l % 3 ) % 3 ; i ++ ) bin = '0' + bin ; Map < String , Character > bin_oct_map = new HashMap < > ( ) ; bin_oct_map . put ( "000" , '0' ) ; bin_oct_map . put ( "001" , '1' ) ; bin_oct_map . put ( "010" , '2' ) ; bin_oct_map . put ( "011" , '3' ) ; bin_oct_map . put ( "100" , '4' ) ; bin_oct_map . put ( "101" , '5' ) ; bin_oct_map . put ( "110" , '6' ) ; bin_oct_map . put ( "111" , '7' ) ; int i = 0 ; String octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ( ) ) break ; } return octal ; } static String decToOctal ( int n ) { String bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } public static void main ( String [ ] args ) { int n = 151 ; System . out . println ( decToOctal ( n ) ) ; } }