Showing posts with label decimal to binary. Show all posts
Showing posts with label decimal to binary. Show all posts

Thursday, September 15, 2011

Decimal to Binary, Decimal to Octal, Decimal to Hexadecimal


Convert Decimal to Binary, Decimal to Octal, Decimal to Hexadecimal using C Programming.

void main()


{
int bin[20],oct[20];
char hex[20];
int i=0,j=0,h=0,k,m,l,dec,dec1,dec2,r;
clrscr();


printf("\n\n\t\tSay thanks to Ashish Ranjan !!!");

printf("\n\n\t\tEnter your Number : ");
scanf("%d",&dec);
printf("\n\t\tDecimal Number : %d\n\n ",dec);


dec1=dec2=dec;
//----------------------Binary--------------------------------------


while( dec != 0)
{
   k=dec/2;
   bin[i]=dec%2;
   dec=k;
   i++;
 //  printf("\n quo = %d,\trem = %d,\tdividend = %d,\ti = %d",k,bin[i-1],dec,i);
}


printf("\n\n\n\t\tBinary Represnetation :  ");
for(i=i;i!=0;i--)
    {printf(" %d",bin[i-1]);}




//----------------------Octal---------------------------------------
 printf("\n\n\n");
while( dec1 != 0)
{
   m=dec1/8;
   oct[j]=dec1%8;
   dec1=m;
   j++;
//  printf("\n quo = %d,\trem = %d,\tdividend = %d,\tj = %d",m,oct[j-1],dec1,j);
}


printf("\n\n\t\tOctal Represnetation :  ");
for(j=j;j!=0;j--)
    {printf(" %d",oct[j-1]);}
//------------------------HexaDecimal-------------------------------


 printf("\n\n\n");
while( dec2 != 0)
{
   l=dec2/16;
   r=dec2%16;
    if(r<10)
   { hex[h]=r; }
   else if(r==10)
   { hex[h]='A';}
   else if(r==11)
   { hex[h]='B';}
   else if(r==12)
   { hex[h]='C'; }
   else if(r==13)
   { hex[h]='D'; }
   else if(r==14)
   { hex[h]='E'; }
   else if(r==15)
   { hex[h]='F'; }


   dec2=l;
   h++;
//  printf("\n quo = %d,\trem = %d,\tdividend = %d,\th = %d\thd = %d",l,r,dec2,h,hex[h-1]);
}


printf("\n\n\t\tHexadecimal Represnetation :  ");
for(h=h;h!=0;h--)
    { if(hex[h-1]>10)
      printf(" %c",hex[h-1]);
      else
      printf(" %d",hex[h-1]);
    }
 getch();
}