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

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

The program to convert a number from octal to binary is discussed here. Firstly, the given octal number is converted to a decimal number. Then, the decimal number is converted to binary.


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


For example, consider the octal number 17 which has to be converted to binary.nnThe decimal equivalent of the octal number 17 is calculated as followsnn7 * 8^0 = 7n1 * 8^1 = 8nnDecimal equivalent : 15 (8 + 7)nnNow, this 15 is converted to binarynn15 / 2 = 7 , rem = 1n7 / 2 = 3 , rem = 1n3 / 2 = 1 , rem = 1n1 / 2 = 0 , rem = 1nnBinary Equivalent : 1111n


Algorithm to convert a number from octal to binary

Firstly, convert the number from octal to decimal.

  • Input the octal number.
  • Count the total number of digits in the given number.
  • Assume that the number has n digits.
  • Multiply each digit in the number with 8^(n-1), when the digit is in the nth position.
  • Perform addition of all the digits after multiplication.
  • The added sum gives the decimal equivalent of the octal number.



convert-a-number-from-octal-to-decimal

Then, convert the decimal number to binary,

  • Divide the number by 2.
  • Note the remainder during each division.
  • Continue to divide until the number becomes 0.
  • The Binary equivalent will be the reverse of the remainder.



convert-a-number-from-octal-to-binary


convert-a-number-from-decimal-to-binary

Program to convert a number from octal to binary


@@coding::1@@


Time complexity:O(n)



Recommended Programs









convert-a-number-from-decimal-to-binary

c