Hi Guys,

I have written the below program to convert binary to octal, it is giving me erroneous results. On entering 1111 as the input number I should get 17 as the output, whereas I am getting 16707000337 as the output. Kindly help me out, below is my program:

Code:

/*program to convert binary to octal*/
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int bin,dec[100]={0},i=0,j=0,k=0,num=0,output[100]={0},number=0,rem1,rem,output1;
printf("enter the binary number to be converted into octal");
scanf("%d",&bin);
rem1=bin;
/*counts the number of digits in the input binary number*/
while(rem1!=0)
{
rem1=rem1/10;
num+=1;
}
/*converts binary number to decimal */
for(i=0;i<num;i++)
{
number=bin%10;
bin=bin/10;
dec[i]=pow(2,j)*number;
j++;
}
/*converts binary number to decimal */
for(;i>=0;i--)
{
output1+=dec[i];
}
/*converts decimal to octal*/
while(output1!=0)
{
rem=output1%8;
output1=output1/8;
output[k]=rem;
k+=1;
}
/*prints the value of octal*/
printf("the octal representation is");
for(;k>0;k--)
{
printf("%d",output[k-1]);
}
system("pause");
return 0;
}

Thank you.