Convert a number from decimal to octal

We can convert any decimal number to its equivalent octal number. In this article we will see some program which  can convert a number from decimal to octal.

The base of decimal number is 10 and we use the digits from 0 to 9 to write any decimal number. On the other hand the base of octal number is 8 where we use the digits 0 to 7 for writing any octal number. However we can convert any decimal number to octal number and in this guide we will write some C program to serve this purpose.

If you don’t know the rules to convert a decimal number to its equivalent octal number then learn how to convert decimal to octal mathematically.

C program to convert decimal to octal

// convert a number from decimal to octal in C

#include <stdio.h>

int main(){
  int decNum, octNum = 0, temp = 1;

  printf("Enter decimal number to convert : ");
  scanf("%d", &decNum);

  while (decNum != 0){
    octNum = octNum + (decNum % 8) * temp;
    decNum = decNum / 8;
    temp = temp * 10;
  }

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

  return 0;
}

Output of decimal to octal program:

decimal-to-octal-in-c-c-program-to-convert-decimal-to-octal

Convert decimal to octal using function

// convert decimal to octal using function in C

#include <stdio.h>

int func_dec_o(int de);

int main(){
  int deci, octal;

  printf("Enter the decimal number to convert : ");
  scanf("%d", &deci);

  octal = func_dec_o(deci);
  printf("\nBinary equivalence is = %d\n", octal);

  return 0;
}

int func_dec_o(int de){
  int oc = 0, t = 1;

  while (de != 0){
    oc = oc + (de % 8) * t;
    de = de / 8;
    t = t * 10;
  }
  return oc;
}

Program output

Enter the decimal number to convert : 72

Binary equivalence is = 110

Previous pageBinary to decimal

Next pageOctal to decimal