C++ Programming Code Examples C++ > Conversions Code Examples C++ Program to Convert Binary Number to Octal and vice-versa C++ Program to Convert Binary Number to Octal and vice-versa In this example, you will learn to convert binary number to octal, and octal number to binary manually by creating a user-defined function. In this program, we will first convert the binary number to decimal. Then, the decimal number is converted to octal. The binary number entered by the user is passed to convertBinaryToOctal() function. And, this function converts the number to octal and returns to the main() function #include <iostream> #include <cmath> using namespace std; int convertBinarytoOctal(long long); int main() { long long binaryNumber; cout << "Enter a binary number: "; cin >> binaryNumber; cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber) << " in octal "; return 0; } int convertBinarytoOctal(long long binaryNumber) { int octalNumber = 0, decimalNumber = 0, j = 0; while(binaryNumber != 0) { decimalNumber += (binaryNumber%10) * pow(2,j); ++j; binaryNumber/=10; } j = 1; while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * j; decimalNumber /= 8; j *= 10; } return octalNumber; }