Convert a decimal number into octal number - C Program

Write a C program to convert a decimal number into octal number using array.

Answer:

The decimal numbers are 0 - 9 with base 10. The octal number are 0 - 8 and the base of octal numbers is 8.

This program shows the conversion of decimal number to octal number.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 20
void convert(int);
int main()
{
     int n,i;
     printf("Enter a decimal number=");
     scanf("%d", &n);
     convert(n);
     return(0);
}
void convert(int n)
{
     int a[MAX], i, j, rem;
     i=0;
     while(n!=0)
     {
          rem=n%8;
          a[i]=rem;
           i++;
          n=n/8;
     }
     printf("\nThe octal number is=");
     for(j=i-1; j>=0; j--)
     {
           printf("%d", a[j]);  
      }
}


Output:

decimal to octal number