Convert Octal to Binary
Convert Octal to Binary in C++ (5 Programs)
IntermediateTopic: Advanced Number Programs
C++ Convert Octal to Binary Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <cmath>
using namespace std;
int octalToDecimal(long long octal) {
int decimal = 0, i = 0, remainder;
while (octal != 0) {
remainder = octal % 10;
octal /= 10;
decimal += remainder * pow(8, i);
++i;
}
return decimal;
}
long long decimalToBinary(int decimal) {
long long binary = 0;
int remainder, i = 1;
while (decimal != 0) {
remainder = decimal % 2;
decimal /= 2;
binary += remainder * i;
i *= 10;
}
return binary;
}
int main() {
long long octal;
cout << "Enter an octal number: ";
cin >> octal;
int decimal = octalToDecimal(octal);
long long binary = decimalToBinary(decimal);
cout << "Octal: " << octal << " = Binary: " << binary << endl;
return 0;
}Output
Enter an octal number: 12 Octal: 12 = Binary: 1010
Understanding Convert Octal to Binary
This program converts octal to binary by first converting to decimal, then to binary. It demonstrates 5 different methods: two-step conversion, direct conversion, using bitset, using string manipulation, and using functions.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.