C++ Program for Binary to Octal conversion

Program for Binary to Octal conversion

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

  • grouping the binary number into pairs of three and converting into octal.
  • By converting the binary number into a decimal number first and then convert that decimal number into an octal number.

We are using second approach, this approach is long but it is very easy to implement and also the chances of error are also very minimum.

Example:10110

Decimal number     =1*(2^4) + 0*(2^3) + 1*(2^2) + 1*(2^1)+0*(2^0) = 16 + 0 + 4 + 2 + 0  22

Octal conversion    → 22/8, Remainder = 6, Quotient = ,→  2/8, Remainder = 2, Quotient = 0

i.e 10110 in binary is 26 in octal.

program to convert binary to octal

Algorithm:-

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

                                  rem = binary %10;

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

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

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

                                decimal += res;

     4. Another loop is started to convert decimal into an octal number.

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

                               rem = decimal % 8;

                          2. The octal number is constructed using the remainder.

                               octal += rem * i;

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

          while (decimal != 0)

     6. octal is returned and printed as output.

C++ Code:-

    
    //C++ Program
    //binary to octal conversion
    #include <iostream>
    #include <math.h>
    using namespace std;
    //Function to convert binary to octal
    int convert(long binary)
    {
        int octal = 0decimal = 0i = 0,rem;
        //converting binary to decimal
        while(binary != 0)
        {
            rem = binary % 10;
            int res = rem * pow(2,i);
            decimal += res;
            i++;
            binary/=10;
        }
        i = 1;
        //converting decimal to octal
        while (decimal != 0)
        {
            rem = decimal % 8;
            octal += rem * i;
            decimal /= 8;
            i *= 10;
        }
        return octal;
    }
    //main program
    int main()
    {
        long binary;
        cout << “Enter a binary number: “;
        //user input
        cin >> binary;
        //calling function
        int octal=convert(binary);
        //printing output
        cout << binary << ” in binary = “ << octal << ” in octal “;
        return 0;
    }

    Output

    Enter a binary number: 10101
    10101 in binary = 25 in octal
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