Octal is a number system which have 8 characters in its counting that are 0,1,2,3,4,5,6,7
Each Character of Octal System is Equal to 8 Digit Binary…. like 0 = 000 , 3 = 011 , 5 = 101, 7 = 111
Table of Number Systems is Given Below
To Convert binary number to a Octal number.. we simply need to extend our binary number and make sure digits are multiple of 3…
for Example if we are given with 1100101 .. we have to append 0’s on it’s left side to make it of Length 9 because 9 is multiple of 3..
So 1100101 will become 001100101 … now starting from right side, we need to make chunks of it of length 3…
So it will become 001 100 101 ,,,,,,,, now in table find Equivalent Character in Table… 001 is 1… and 100 is 4 and 101 is 5… so it will become 145.
Hence 1100101 is 145 in Octal System
Another way to Do it is to Convert Binary to Decimal and then Take LCM of Decimal Number with 8 to get it’s Octal Equivalent..
For Example 1100101 as we know is equal to 101 in Decimal… So taking LCM of 101 with 8
8 | 101
8 | 12 — 5
8 | 1 — 4
So 145 is Answer..
Code of Binary to Octal in C++
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
char arr[100];
long int dec=0,i,j;
int oct[50];
cout<<"Enter binary "<<endl;
cin>>arr;
for(i=0;arr[i]!='\0';i++);
for(i=i-1,j=0;i>=0;i--,j++)
{
dec+=(arr[j]-48)*pow(2,i);
}
for(i=0;dec>0;i++)
{
oct[i]=dec%8;
dec=dec/8;
}
cout<<"\nOctal equivalent"<<endl;
for(i=i-1;i>=0;i--)
cout<<oct[i];
}
Output:
Enter binary
1100101
Octal equivalent
145