 # C++ Programming Code Examples

## C++ > Conversions Code Examples

### Program to Convert Octal Number to Binary Number

``` Program to Convert Octal Number to Binary Number 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, the octal number is converted to decimal at first. Then, the decimal number is converted to binary number. The octal number entered by the user is passed to convertOctalToBinary() function. And, this function converts the number to binary and returns the main() function #include <iostream> #include <cmath> using namespace std; long long convertOctalToBinary(int); int main() { int octalNumber; cout << "Enter an octal number: "; cin >> octalNumber; cout << octalNumber << " in octal = " << convertOctalToBinary(octalNumber) << "in binary"; return 0; } long long convertOctalToBinary(int octalNumber) { int decimalNumber = 0, i = 0; long long binaryNumber = 0; while(octalNumber != 0) { decimalNumber += (octalNumber%10) * pow(8,i); ++i; octalNumber/=10; } i = 1; while (decimalNumber != 0) { binaryNumber += (decimalNumber % 2) * i; decimalNumber /= 2; i *= 10; } return binaryNumber; } ``` 