Wednesday, 16 April 2014

Program to Convert a Binary Number to Its Octal Equivalent.

main()
{
 int bin[16],i,oct[16],s,os,j,x,r;
 clrscr();
 printf("Enter the number of bits you want to insert. --> ");

 scanf("%d",&s);
Flowchart of Binary to Octal Conversion
 printf("Enter Binary number with space between each bit.\n");
 for(i=0;i<s;i++)
 {
  scanf("%d",&bin[i]);
  if(bin[i]>1)
  {
   printf("\nThis is not a binary number.");
   getch();
   exit(0);
  }
 }
 printf("\nYour Binary number is ");
 for(i=0;i<s;i++)
 printf("%d",bin[i]);
 if(s%3!=0)
  os=(s/3)+1;
 else
  os=s/3;
 j=s-1;
 for(i=os-1;i>=0;i--)
 {
  x=1;r=0;
  for(;x<=4 && j>=0;j--)
  {
   r=r+bin[j]*x;
   x*=2;
  }
  oct[i]=r;
 }
 printf("\nOctal equivalent of your binary number is ");
 for(i=0;i<os;i++)
  printf("%d",oct[i]);
 getch();
}

OutPut:
Binary to Octal Conversion C Program's Output

No comments:

Post a Comment