Decimal to Octal
Algorithm
Input A decimal number Output An octal number complexity O(d)where d is the number of digits in the number Decimal_to_octal(num) 1 while (num>0) then 2 a[i] =num%8; 3 num= num/8; 4 i=i+1; 5 Print a[i] in reverse order
Algorithm Description
This algorithm takes an integer and converts it to octal number. The octal number is generated by repeatedly dividing the given decimal number by 8 and storing the remainders till number is greater then zero. The remainders are printed in reverse order of their occurence.
C Code
//converting from decimal to octal number system
//Input Any positive integer upto 32767
#include< stdio.h>
#include< conio.h>
int main()
{
int num,i,a[20],n;
printf("Enter a decimal number ");
scanf("%d",&num);
printf("Octal equivalent of %d is ",num);
i=0;
while(num> 0)
{
a[i]=num%8;
num=num/8;
i++;
}
for(n=i-1;n> =0;n--)
printf("%d",a[n]);
}