By | 25.12.2016

# Convert Binary Number to Octal

Write a C++ Program to Convert Binary Number to Octal. Here’s simple C++ Program to Convert Binary Number to Octal in C++ Programming Language.

## Numbers in C++

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types.

Here is source code of the C++ Program to Convert Binary Number to Octal. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

### SOURCE CODE : :

```/* C++ Program to Convert Binary Number to Octal  */

#include <iostream>
#include <cmath>

using namespace std;

int convertBinarytoOctal(long long);
int main()
{
long long binaryNumber;

cout << "Enter a binary number: ";
cin >> binaryNumber;

cout <<"\n [ "<< binaryNumber << " ] in binary = [ " << convertBinarytoOctal(binaryNumber) << " ] in octal \n";

return 0;
}

int convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;

while(binaryNumber != 0)
{
decimalNumber += (binaryNumber%10) * pow(2,i);
++i;
binaryNumber/=10;
}

i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}```

### OUTPUT : :

```/* C++ Program to Convert Binary to Octal  */

Enter a binary number: 100111

[ 100111 ] in binary = [ 47 ] in octal

Process returned 0```

Above is the source code for C++ Program to Convert Binary to Octal which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

3 1 vote
Article Rating
Category: C++ Programming Number Programs Tags: My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of 1 Comment
Inline Feedbacks #include <iostream>
using namespace std;

int main(){

int bin, dig, b=1, decim=0, oct;

cout << Enter any binary number: ;
cin >> bin;

cout << \n[ << bin << ] in binary = ;

while(bin!=0){ // pass binary to decimal
dig=bin%10;
decim+=dig*b;
b=b*2;
bin=bin/10;
}

oct = (decim/8)*10+decim%8; // pass decimal to octal

cout << [ << oct << ] in octal\n;
}