# C program to convert decimal toOctalnumber

## C program to convert decimal to Octal number

This page has a C program to convert decimal to Octal number . We can write the C program to convert the given mixed decimal number into its equivalent Octal. For Integer part conversion to Octal divide the given integer part of the given decimal number by 8 again and again until the Integer part of the decimal number can be divided by 8 and print the remainders for each division (form bottom to top) to form the Octal equivalent. For decimal part conversion, multiply the given decimal part (number after the decimal point) of the decimal number by 8 again and again for atleast 5 or 7 steps(times) and print the carries for each multiplication (form top to bottom) to form the Octal equivalent.  The below given C program to convert a given mixed decimal number into its equivalent Octal numberKindly check out the program to convert decimal to Octal number.

### Decimal to Octal number Conversion

#include<stdio.h>
#include<string.h>
void octalinteger(long int);
void octalfractional(long int,int);
int power(int,int);
int main(){ int a=0,j=0,m,n=0,q=1;
long int k1,k2;
char g1[80], p1[80]={'\0'}, p2[80]={'\0'};
clrscr();
printf("Give a decimal number : ");
gets(g1);
for (a = 0; g1[a] != '\0'; a++)
{
q++;
}
for(m=0;m<q-1;m++){
if(g1[m] == '.')
break;
p1[m]=g1[m];
}
for(a=m+1;a<=q-1;a++){
p2[n]=g1[a];
n++;
j++;
}
k1= atoi(p1);
k2= atoi(p2);
printf("\n\n The given decimal number is : %s \n\n", g1);
octalinteger(k1);
for(m=0;m<q-1;m++){
if(g1[m] == '.'){
printf(".");
break;
}
}
if(g1[m] == '.')
octalfractional(k2, j-1);
getch();
return(0);
}

void octalinteger(long int s){
int b=1, h, remai[40];
printf("\n\n The Octal number is : ");
while(s>0){
remai[b] = s % 8;
s = s / 8;
b++;
}
b--;
for(h=b;h>=1;h--)
printf("%d", remai[h]);
return(0);
}

void octalfractional(long int e, int u){
int a,r;
int p;
for(a=1;a<=8;++a){
e=e*8;
p=power(10,u);
if(e>=p){
r = e / p;
e = e % p;
printf("%d",r);
}
else
printf("%d",0);
}
return(0);
}

int power(int x,int n){
int i;
int num = 1;
for (i = 0; i < n; ++i)
num *= x;
return(num);
}

Sample Output:

Give a decimal number : 486.176

The given decimal number is : 486.176

The Octal number is :  746.13207126