C++ Program for Binary to Octal conversion
Binary to Octal conversion in C++
Here, we will discuss binary to octal conversion in C++.There are two approaches of converting an octal number into a binary number like
- grouping the binary number into pairs of three and converting into octal.
- By converting the binary number into a decimal number first and then convert that decimal number into an octal number.
We are using second approach, this approach is long but it is very easy to implement and also the chances of error are also very minimum.
Method 1:-
We will first convert the Binary number into Decimal and then convert the Decimal number into Octal
Make sure that you have gone through these approaches below –
Method 1 Code
#include<iostream> #include<math.h> using namespace std; // function to convert binary to octal void convert(long long num) { int octalDigit = 0, count = 1, i = 0, pos = 0; int octalArray[32] = {0}; while(num != 0) { int digit = num % 10; octalDigit += digit * pow(2, i); i++; num /= 10; // placing current octalsum for 3 pair in array index position octalArray[pos] = octalDigit; // whenever we have read next 3 digits // setting values to default // increasing pos so next values can be placed at next array index if(count % 3 == 0) { octalDigit = 0; i = 0; pos++; } count++; } // printing octal array in reverse order for (int j = pos; j >= 0; j--) cout<<octalArray[j]; } //main program int main() { // long used rather than int to store large values long long binary; printf("Enter binary number: "); scanf("%lld", &binary); convert(binary); return 0; }
Output :
Enter binary number: 1010
Decimal : 10
Octal : 12
Method 2 Code
#include<iostream> #include<math.h> using namespace std; // function to convert binary to octal void convert(long long num) { int octalDigit = 0, count = 1, i = 0, pos = 0; int octalArray[32] = {0}; while(num != 0) { int digit = num % 10; octalDigit += digit * pow(2, i); i++; num /= 10; // placing current octalsum for 3 pair in array index position octalArray[pos] = octalDigit; // whenever we have read next 3 digits // setting values to default // increasing pos so next values can be placed at next array index if(count % 3 == 0) { octalDigit = 0; i = 0; pos++; } count++; } // printing octal array in reverse order for (int j = pos; j >= 0; j--) cout<<octalArray[j]; } //main program int main() { // long used rather than int to store large values long long binary; printf("Enter binary number: "); scanf("%lld", &binary); convert(binary); return 0; }
Output :
Enter binary number: 010101
25
Method 2:-
Method 2 uses the concept of grouping 3 successive digits/bits of the binary number and calculating octal digits against each grouping
We use an additional array to store the octal digits at each index.
We will need to print the array in reverse to get actual octal equivalent.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Highest Common Factor(HCF): C | C++ | Java | Python
- Lowest Common Multiple (LCM) : C | C++ | Java | Python
- Greatest Common Divisor : C | C++ | Java | Python
- Binary to Decimal to conversion : C | C++ | Java | Python
- Octal to Decimal conversion : C | C++ | Java | Python
- Hexadecimal to Decimal conversion: C | C++ | Java | Python
- Decimal to Binary conversion: C | C++ | Java | Python
- Decimal to Octal Conversion: C | C++ | Java | Python
- Decimal to Hexadecimal Conversion: C | C++ | Java | Python
- Binary to Octal conversion : C | C++ | Java | Python
- Octal to Binary conversion : C | C++ | Java | Python
- Quadrants in which a given coordinate lies : C | C++ | Java | Python
- Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
- Maximum number of handshakes: C | C++ | Java | Python
- Addition of two fractions: C | C++ | Java | Python
- Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
- Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
- Count possible decoding of a given digit sequence : C | C++ | Java | Python
- Calculate the area of a circle : C | C++ | Java | Python
- Find the prime numbers between 1 to 100 : C | C++ | Java | Python
- Calculate the number of digits in an integer : C | C++ | Java | Python
- Convert digit/number to words : C | C++ | Java | Python
- Counting number of days in a given month of a year: C | C++ | Java | Python
- Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
- Finding number of integers which has exactly x divisors: C | C++ | Java | Python
- Finding Roots of a quadratic equation : C | C++ | Java | Python
//using if else updated one
#include
using namespace std;
int main() {
int num;
cout << "Enter a binery number" <> num;
if (num == 000) {
cout << "0";
}
else if (num == 001) {
cout << "1";
}
else if (num == 010 || num == 10) {
cout << "2";
}
else if (num == 011 || num == 11) {
cout << "3";
}
else if (num == 100) {
cout << "4";
}
else if (num == 101) {
cout << "5";
}
else if (num == 110) {
cout << "6";
}
else if (num == 111) {
cout << "7";
}
else {
cout << "Not an octal number" << endl;
cout << "Please enter binery number between 000 to 111" <0″);
return 0;
}
//using if else
#include
using namespace std;
int main() {
int num;
cout << "enter a binery number"<> num;
if ( num == 0) {
cout << "0";
}
else if ( num == 001 ) {
cout << "1";
}
else if (num == 010 || num == 10) {
cout << "2";
}
else if (num == 011 || num == 11) {
cout << "3";
}
else if (num == 100) {
cout << "4";
}
else if (num == 101) {
cout << "5";
}
else if (num == 110) {
cout << "6";
}
else {
cout <0″);
return 0;
}