C Program to Convert Decimal to Octal

0

Method 1

#include<stdio.h>
 
#include<conio.h>
 
#include<math.h>
  
void dec_oct(long int num)   // Function Definition
{
 
long int rem[50],i=0,length=0;
 
while(num>0)
 
 {
 
 rem[i]=num%8;
 
 num=num/8;
 
 i++;
 
 length++;
 
 }
 
printf("nOctal number : ");
 
     for(i=length-1;i>=0;i--)
 
             printf("%ld",rem[i]);
 
}
 
void main()
 
{
 
long int num;
 
clrscr();
  
 printf("Enter the decimal number : ");
 
 scanf("%ld",&num);
  
    dec_oct(num);   // Calling function
  
 getch();
 
}

Output

Enter the decimal number : 20
Octal number : 24

Method 2

#include<stdio.h>
 
#include<conio.h>
 
void main()
 
{
    clrscr();
 
    long int decnum, rem, quot;
 
    int i=1, j, octnum[100];
 
    printf("Enter any decimal number : ");
 
    scanf("%ld",&decnum);
 
    quot=decnum;
 
    while(quot!=0)
    {
 
        octnum[i++]=quot%8;
 
        quot=quot/8;
 
    }
 
    printf("Equivalent octal value of %d is : \n",decnum);
 
    for(j=i-1; j>0; j--)
 
    {
        printf("%d",octnum[j]);
    }
 
    getch();
}

Output

Enter any decimal number : 50
Equivalent octal calue of 50 is 
62

Leave a Reply

Your email address will not be published. Required fields are marked *