PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Conversion

Program to convert a binary number into octal number

Input - Output

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

Algorithm

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

CODE


/* 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;
}

OutPut

Enter a Binary number : 1011

Octal equivalent of given number : 13