Decimal to octal conversion with minimum use of arithmetic operators | C ++ 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 program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } string convertBinToOct ( string bin ) { int l = bin . size ( ) ; for ( int i = 1 ; i <= ( 3 - l % 3 ) % 3 ; i ++ ) bin = '0' + bin ; unordered_map < string , char > bin_oct_map ; bin_oct_map [ "000" ] = '0' ; bin_oct_map [ "001" ] = '1' ; bin_oct_map [ "010" ] = '2' ; bin_oct_map [ "011" ] = '3' ; bin_oct_map [ "100" ] = '4' ; bin_oct_map [ "101" ] = '5' ; bin_oct_map [ "110" ] = '6' ; bin_oct_map [ "111" ] = '7' ; int i = 0 ; string octal = " " ; while ( 1 ) { octal += bin_oct_map [ bin . substr ( i , 3 ) ] ; i += 3 ; if ( i == bin . size ( ) ) break ; } return octal ; } string decToOctal ( int n ) { string bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } int main ( ) { int n = 151 ; cout << decToOctal ( n ) ; return 0 ; }