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.
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 */
using namespace std;
int convertBinarytoOctal(long long);
long long binaryNumber;
cout << "Enter a binary number: ";
cin >> binaryNumber;
cout <<"\n [ "<< binaryNumber << " ] in binary = [ " << convertBinarytoOctal(binaryNumber) << " ] in octal \n";
int convertBinarytoOctal(long long binaryNumber)
int octalNumber = 0, decimalNumber = 0, i = 0;
while(binaryNumber != 0)
decimalNumber += (binaryNumber%10) * pow(2,i);
i = 1;
while (decimalNumber != 0)
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
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.
Thanks for reading the post….