C++ program to convert binary number to octal and its vice verse:Here is a C++ program which convert the binary number into octal manually.
C++ program to convert binary number to octal and its vice verse
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int oc,p,r,ch,n,a,i=0,x=0,d=0,j,o;
long int b,s=0,sum=0;
cout<<"Enter Choice"<<endl;
cout<<"1.B-O"<<endl<<"2.O-B"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"ENTER THE BINARY DIGIT :"<<endl;
cin>>b;
while(b>0)
{
a=b%1000;
j=0;
d=0;
while(a>0)
{
n=(a%10);
d=d+(n*pow(2,j));
j=j+1;
a=a/10;
}
x=x+(d*pow(10,i));
i=i+1;
b=b/1000;
}
cout<<"Answer ="<<x;
break;
case 2:
cout<<"Enter the Octel"<<endl;
cin>>oc;
i=0;
j=0;
sum=0;
s=0;
while(oc>0)
{
p=oc%1000;
while(p>0)
{
r=p%2;
s=s+r*pow(10,j);
j++;
p=p/2;
}
sum=sum+(s*pow(1000,i));
i++;
oc=oc/10;
j=0;
}
cout<<"Binary ="<<sum;
break;
default:
cout<<"????????????????????????"<<endl;
break;
}
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int oc,p,r,ch,n,a,i=0,x=0,d=0,j,o;
long int b,s=0,sum=0;
cout<<"Enter Choice"<<endl;
cout<<"1.B-O"<<endl<<"2.O-B"<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"ENTER THE BINARY DIGIT :"<<endl;
cin>>b;
while(b>0)
{
a=b%1000;
j=0;
d=0;
while(a>0)
{
n=(a%10);
d=d+(n*pow(2,j));
j=j+1;
a=a/10;
}
x=x+(d*pow(10,i));
i=i+1;
b=b/1000;
}
cout<<"Answer ="<<x;
break;
case 2:
cout<<"Enter the Octel"<<endl;
cin>>oc;
i=0;
j=0;
sum=0;
s=0;
while(oc>0)
{
p=oc%1000;
while(p>0)
{
r=p%2;
s=s+r*pow(10,j);
j++;
p=p/2;
}
sum=sum+(s*pow(1000,i));
i++;
oc=oc/10;
j=0;
}
cout<<"Binary ="<<sum;
break;
default:
cout<<"????????????????????????"<<endl;
break;
}
getch();
}