C Program to Convert Decimal to Octal

Decimal to octal in C : Write a C program to accept a decimal number and print its octal equivalent.

Algorithm

  1. Accept a decimal number as input.
  2. Divide the decimal number by 8 and obtain its remainder and quotient. Store the remainder in the array.
  3. Repeat the step 2 with the quotient obtained. Do this until the quotient becomes zero.
  4. Print the array in the reverse order to get the output.

C Program to Convert Decimal to Octal

/* Aim: C program to Convert Decimal number to Octal number*/

#include<stdio.h>

int main()
{
  long dec, rem, q; // q for quotient
  int i = 1, j, octal[100];

  printf("\n Enter a decimal number:- ");
  scanf("%ld", &dec);

  q = dec;

  while (q != 0)
  {
	octal[i++] = q % 8;
	q = q / 8;
  }

  printf("\n Equivalent octal value is ");

  for (j = i - 1; j > 0; j--)
	printf("%d", octal[j]);

  return 0;
}

/* Output of above code:-

Enter the decimal number:- 75
Equivalent octal value is 113

*/