HomeC++C++ Program to Convert Binary Number to Octal and vice-versa

C++ Program to Convert Binary Number to Octal and vice-versa

In this post, you will learn how to Convert Octal Number to Decimal and vice-versa using C++ programming language.

This lesson will teach you how to Convert Binary Number to Octal and vice-versa, mathematical operations, loop statements and logical operators statement using the C++ Language. Let’s look at the below source code.

How to Convert Binary Number to Octal and vice-versa?

RUN CODE SNIPPET

Source Code

#include <iostream>
#include <cmath>
using namespace std;
int BinarytoOctal(int binaryNum) 
{
   int octalNum = 0, decimalNum = 0, count = 0;
   while(binaryNum != 0) 
   {
      decimalNum += (binaryNum%10) * pow(2,count);
      ++count;
      binaryNum/=10;
   }
   count = 1;
   while (decimalNum != 0) 
   {
      octalNum += (decimalNum % 8) * count;
      decimalNum /= 8;
      count *= 10;
   }
   return octalNum;
}
int OctalToBinary(int octalNum) 
{
   int decimalNum = 0, binaryNum = 0, count = 0;
   while(octalNum != 0) 
   {
      decimalNum += (octalNum%10) * pow(8,count);
      ++count;
      octalNum/=10;
   }
   count = 1;
   while (decimalNum != 0) 
   {
      binaryNum += (decimalNum % 2) * count;
      decimalNum /= 2;
      count *= 10;
   }
   return binaryNum;
}
int main() 
{
   int binaryNum = 1011, octalNum = 25;
   cout <<"Binary to Octal"<<endl;
   cout<<"\nBinary number: "<<binaryNum<<endl;
   cout<<"Octal number: "<<BinarytoOctal(binaryNum)<<endl;
   cout <<"\nOctal to Binary"<<endl;
   cout<<"\nOctal number: "<<octalNum<<endl;
   cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl;
   return 0;
}

Output

Binary to Octal
Binary number: 1011
Octal number: 13
Octal to Binary
Octal number: 25
Binary number: 10101

The statements #include<iostream>, using namespace std are the main factors that support the function of the source code. The void keyword is used when the function does not have a return value.  The #include <cmath> is used to enable additional mathematical functions in the code.

Now we can look into the working and layout of the code’s function.

  1. In this code we have two sections one to convert a Binary to Octal and one to convert a Octal to Binary.
  2. The first section is we execute the Binary to Octal function.
  3. Declare the string values as integers and assign the values as 0 and open while loop with the condition (binaryNum != 0)
  4. In the loop statement declare the following functions:
    { decimalNum += (binaryNum%10) * pow(2,count); ++count; binaryNum/=10; }First perform the mathematical functions; reminder of an integer and 10 multiplied with the power of 2 and the integer. Then perform the AND addition assignment operation with the integer decimalNum
    .
    Increment the integer count, and perform the AND division assignment operator.
    Assign the value count to zero. This section converts the Binary Number to Octal Number
  5. Open another while loop with the condition (decimalNum != 0) and in the loop statement declare the following operations.
    { octalNum += (decimalNum % 8) * count; decimalNum /= 8; count *= 10; }
  6. After executing the above loop statement using the return statement return the integer octalNum
  7.  The next section we execute the Octal to Binary function.
  8. Declare the variables as integers and assign the values as 0 and similar to the previous section declare two while loop with the condition and the loop statement to convert the Octal Number to a Binary Number.
  9. The final section with int main displays the answers using the output statements cout .

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Share:

Leave a Reply

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022