Convert a number from binary to octal in C, C++, Java and Python | faceprep

Convert a number from binary to octal in C, C++, Java and Python | faceprep

The program to convert a number from binary to octal using C, C++, Java and other programming languages is discussed here.


Convert a number from binary to octal in C, C++, Java and Python


Firstly let us look at an example to do this.


For example,nnConsider the binary number 1111 which has to be converted to octal.nDivide the binary number into groups of three from right to left.nn1111 can be grouped as 001 111nnNow, find the octal number for each group.nnThree-bit binary numbers from (000 to 111) have equal decimal and octal representations. Hence, the octal numbers for binary numbers in the range (000 to 111) are same as nthat of the decimal numbers.nnThe octal number of 001 is 1 and 111 is 7.nnThe octal equivalent of 1111 is 17.n


Algorithm to convert a number from binary to octal

  • Input the binary number.
  • Divide the binary number into groups of three from right to left.
  • Find the decimal equivalent of each group.
  • The decimal equivalent of each group from left to right gives the equivalent octal number of the given binary number.


Convert a number from binary to octal in C, C++, Java and Python

Program to convert a number from binary to octal


@@coding::1@@


Time complexity: O(n)


Recommended Programs








Convert a number from binary to octal in C, C++, Java and Python

c