Binary to octal number conversion

Binary number is 2 based number where octal is the 8 based number. Binary to octal conversion and some programs related to that will be discussed here. In this C programming guide we will make some program to convert a number from binary to octal.

As we know, we can use the digits from 0 to 7 for writing any number in octal number system where we can use only 0 and 1 to write any number in binary number system. Here we will take the binary number as input from the user and the convert it to the equivalent octal number using C program. So, let’s see some program related to this.

We recommend you to learn if you don’t know how to convert binary to octal mathematically.

C program to convert binary to octal

// c program to convert from binary to octal

#include <stdio.h>
#include <math.h>

int main(){
  long c, biNum, oct = 0, deci = 0;

  printf("Enter any binary number to convert here : ");
  scanf("%ld", &biNum);

  while(biNum != 0){
    deci = deci + (biNum % 10) * pow(2, c);
    c++;
    biNum /= 10;
  }

  c = 1;

  while (deci != 0){
    oct = oct + (deci % 8) * c;
    deci /= 8;
    c *= 10;
  }

  printf("\nOctal equivalence is = %d\n", oct);

  return 0;
}

Output of binary to octal program

binary-to-octal-conversion-in-c-c-program-to-convert-binary-to-octal

Convert binary to octal using function in C

// convert binary to octal using function in C

#include <stdio.h>
#include <math.h>

int converter_b_to_o(long bin);

int main(){
  long bi_num, oct_num;

  printf("Enter binary number to convert here : ");
  scanf("%ld", &bi_num);

  printf("\nEquivalent octal is = %d\n", converter_b_to_o(bi_num));

  return 0;
}


int converter_b_to_o(long bin){
  int k = 0, oct = 0, deci = 0;

  while(bin != 0){
    deci = deci + (bin % 10) * pow(2, k);
    k++;
    bin /= 10;
  }

  k = 1;

  while (deci != 0){
    oct = oct + (deci % 8) * k;
    deci /= 8;
    k *= 10;
  }

  return oct;
}

Program output

Enter binary number to convert here : 111011001

Equivalent octal is = 731

Previous pageOctal to decimal

Next pageOctal to binary