Octal to binary conversion in C++ by R4R Team

Octal number :
Consists of 8 elements
range : 0 to 7

Binary number :
Consists of 2 elements
range : 0 to 1

Octal to Binary conversion:

program:

#include< iostream>
#include< math.h>
int main() {
int n,num=0,i=0,decimal=0;
cout<<"Enter Octal number"<< endl;
cin>>n;
//Octal to decimal
while(n)
{
num=n%10;
num=num*pow(8,i++);
decimal=decimal+num;
n=n/10;
}
//decimal to binary
num=0;
while(decimal)
{
num=num*10+decimal%2;
decimal=decimal/2;
}
cout<<"Binary is "<< num;
}


output-

Enter octal number
41
Binary is 100001

-In this program, we take an input in the octal form then try to find its binary equivalent.
-Firstly we change octal to decimal and then decimal to binary.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!