The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Monday, September 5, 2011

Binary-Decimal and Decimal-Hexadecimal Conversion


#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max 10
 void main()
  {
   clrscr();
   long bin;
   char hdec[max];
   int i,x,n,fb,dec;
   do
   {
   printf("Enter the Binary No. :- ");
   scanf("%ld",&bin);
   i = fb = dec = 0;
   while(bin)
     {
       n = bin % 10;
       dec += n*pow(2,i);
       i++;
       bin /= 10;
       if( n != 0 && n != 1)
{
  fb = 1;
  break;
}
     }
    if(fb == 1)
     printf("\n\n\t\t ERROR :- This is not a binary number .. Retype Again !! \n\n");
   }while(fb == 1);
   i = 0;
   printf("\n\n\t Its decimal equivalent is :- %d",dec);
   printf("\n\n\t n Its hexadecimal equivalent is :- ");
   while(dec != 0 )
    {
      n = dec%16;
       if(n==10) hdec[i] = 'A';
       else if(n==11)    hdec[i] = 'B';
       else if(n==12)    hdec[i] = 'C';
       else if(n==13)    hdec[i] = 'D';
       else if(n==14)    hdec[i] = 'E';
       else if(n==15)    hdec[i] = 'F';
       else         hdec[i] = n+48 ;
       i++;
      dec /= 16;
    }
   hdec[i]='\0';
   strrev(hdec);
   puts(hdec);

   getch();
  }

No comments:

Post a Comment