C++ Program for Octal to Binary conversion

 

Octal to Binary Conversion

There are 2 approaches of converting an octal number into a binary number like:

  • Write 3 digit binary number for all the digits of a number from left to right. 
  • first convert octal to decimal and then decimal to binary .

We are using the second approach, it is a long but very simple approach which can also be implemented easily with minimum chances of error.

Example: 26

Decimal number         = 2*(8^1) + 6*(8^0)= 16 + 6= 22

Binary conversion    1) 22/2, Remainder = 0, Quotient =11, 

2)  11/2,Remainder = 1, Quotient = 5, 3) 5/2, Remainder = 1, Quotient = 2

4)  2/2, Remainder = 0, Quotient = 1,  5)   1/2, Remainder = 1, Quotient = 0

i.e 26 in octal is 10110 in binary.

 

Octal to binary conversion
Competitive Coding Techniques

Algorithm:-

  1. User gives an input(octal number).
  2. Input is stored in an int type variable say octal.
  3. A function convert() is called using octal as parameter.
    1. A loop is started to convert octal into a decimal number
      1. Digits of octal are extracted one by one starting from right.

                               int rem = octal%10;

                           2. Extracted digits are multiplied with proper base i.e. power of 8.

                               int res = rem*pow(8,i);

                           3. After multiplying each digit with proper base the results are added and stored in another variable say decimal.

                              decimal += res;

                 2. Another loop is started to convert decimal into a binary number

      1. The number is divided by 2 and the remainder is stored.

                               rem = decimal%2;

                          2. The binary number is constructed using the remainder.

                              binary += rem*i;

                3. The loop will run until the number is not equal to 0.

                    while (n!=0)

     4. binary is returned and printed as output.

Octal to Binary conversion in C++

C++ Code:-

    #include <bits/stdc++.h>

using namespace std;

    //Function to convert octal to binary

    long convert(int octal)

    {

        int decimal = 0, i = 0;

        long binary = 0;

        //converting octal to decimal

        while(octal != 0)

        {

            int rem = octal%10;

            int res=rem * pow(8,i);

            decimal += res;

            i++;

            octal/=10;

        }

        i = 1;

        //converting decimal to binary

        while (decimal != 0)

        {

            int rem = decimal % 2;

            binary += rem * i;

            decimal /= 2;

            i *= 10;

        }

        return binary;

    }

    //main program

    int main()

    {

        int octal;

        cout << "Enter an octal number: ";

        //user input

        cin >> octal;

        //function call

        long binary = convert(octal);

        //printing output

        cout << octal << " in octal = " << binary << " in binary";

        return 0;

    }
Output

Enter an octal number: 307

307 in octal = 11000111 in binary
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python