Input
Enter a Binary number : 1011
Output
Octal equivalent of given number : 13
Input
Enter a Binary number : 1110
Output
Octal equivalent of given number : 16
Step 1: START
Step 2: Enter a binary number 'num' as an input.
Step 3: Firstly we have to convert binary number into deciaml form.
Step 4: After that convert into octal form
Step 4: END
/* Binary to octal Conversion */
#include <stdio.h>
#include <math.h>
int main()
{
int bin,decimal=0,rem,i,j,oct[10];
printf(" Enter the binary number : ");
scanf("%d",&bin);
for(i=0;bin>0;i++)
{
rem=bin%10;
decimal=decimal+rem*pow(2,i);
bin/=10;
}
/* binary --> deciaml --> octal */
for(i=0;decimal>0;i++)
{
oct[i]=decimal%8;
decimal/=8;
}
printf(" Octal of given number is = ");
for(j=i-1;j>=0;j--)
printf("%d",oct[j]);
return 0;
}
/* Binary to octal Conversion */
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int bin,decimal=0,rem,i,j,oct[10];
cout<<" Enter the binary number : ";
cin>>bin;
for(i=0;bin>0;i++)
{
rem=bin%10;
decimal=decimal+rem*pow(2,i);
bin/=10;
}
/* binary --> deciaml --> octal */
for(i=0;decimal>0;i++)
{
oct[i]=decimal%8;
decimal/=8;
}
cout<<" Octal of given number is = ";
for(j=i-1;j>=0;j--)
cout<<oct[j];
return 0;
}
Enter a Binary number : 1011
Octal equivalent of given number : 13
advertisment