# Decimal to octal conversion with minimum use of arithmetic operators

Given a decimal number n without floating point. The problem is to convert the decimal number to octal number with minimum use of arithmetic operators.

Examples:

```Input : n = 10
Output : 12
12 is octal equivalent of decimal 10.

Input : n = 151
Output : 227
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Following are the steps:

1. Perform decimal to binary conversion without using arithmetic operators of the given number n. Refer this post. Let this number be bin.
2. Convert the binary number bin to octal. Refer this post.

## C++

 `// C++ implementation of decimal to octal conversion ` `// with minimum use of arithmetic operators ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// function for decimal to binary conversion ` `// without using arithmetic operators ` `string decToBin(``int` `n) ` `{ ` `    ``if` `(n == 0) ` `        ``return` `"0"``; ` `      `  `    ``// to store the binary equivalent of decimal ` `    ``string bin = ``""``;     ` `    ``while` `(n > 0)     ` `    ``{ ` `        ``// to get the last binary digit of the  ` `        ``// number 'n' and accumulate it at the  ` `        ``// beginning of 'bin' ` `        ``bin = ((n & 1) == 0 ? ``'0'` `: ``'1'``) + bin; ` `          `  `        ``// right shift 'n' by 1 ` `        ``n >>= 1; ` `    ``} ` `      `  `    ``// required binary number ` `    ``return` `bin; ` `} ` ` `  `  `  `// Function to find octal equivalent of binary ` `string convertBinToOct(string bin) ` `{ ` `    ``int` `l = bin.size(); ` `          `  `    ``// add min 0's in the beginning to make ` `    ``// string length divisible by 3  ` `    ``for` `(``int` `i = 1; i <= (3 - l % 3) % 3; i++) ` `        ``bin = ``'0'` `+ bin; ` `       `  `    ``// create map between binary and its ` `    ``// equivalent octal code ` `    ``unordered_map 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) ` `    ``{ ` `        ``// one by one extract from left, substring ` `        ``// of size 3 and add its octal code ` `        ``octal += bin_oct_map[bin.substr(i, 3)]; ` `        ``i += 3; ` `        ``if` `(i == bin.size()) ` `            ``break``;         ` `    ``} ` `       `  `    ``// required octal number ` `    ``return` `octal;     ` `} ` ` `  `// function to find octal equivalent of decimal ` `string decToOctal(``int` `n) ` `{ ` `    ``// convert decimal to binary ` `    ``string bin = decToBin(n); ` `     `  `    ``// convert binary to octal ` `    ``// required octal equivalent of decimal ` `    ``return` `convertBinToOct(bin); ` `} ` ` `  `// Driver program to test above ` `int` `main() ` `{ ` `    ``int` `n = 151; ` `    ``cout << decToOctal(n); ` `    ``return` `0; ` `} `

## Java

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

Output:

```227
```

Time Complexity: O(n), where n is the length of the binary string.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.