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 ;)

Tuesday, September 6, 2011

Decimal-Binary and Decimal-Hexadecimal Conversion


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max 10

 class number
  {
   private :
     int dec;
     long bin;
     char hdec[max];
   public  :
     void indec()
      {
       cout<<"Enter any decimal number :- ";
       cin>>dec;
      }

     void dec2bin()
      {
       int i = 0,n,dec2 ;
       bin = 0;
       dec2 = dec;
       while(dec2)
{
n = dec2 % 2;
bin += n*pow(10,i);
i++;
dec2 /= 2;
}
       cout<<"\n\n\t Its binary equivalent is :-"<<bin;
      }

     void dec2hxdc()
      {
       int i=0,n;
       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);
cout<<"\n\n\t n Its hexadecimal equivalent is :- ";
puts(hdec);
      }

     };

 void main()
 {
  clrscr();
  number nm;
  nm.indec();
  nm.dec2bin();
  nm.dec2hxdc();
  getch();
 }

No comments:

Post a Comment