Saturday, December 19, 2009

Binary representation of very large decimal numbers

#include<stdio.h>


#include<conio.h>


void main()


{


      char A[100];


      int i=0,B[100],count=0,rem=0,quo[100],num=0,ans[100],temp,j=0,trav=0,dig=0,test,tru=0,k;


      printf("Enter the number: ");


      scanf("%s",A);


      //Converting the string input to an array of decimal digits.


      for(i=0;A[i]!='';i++)


      {


            B[i]=A[i]-48;


            count++;


      }


      temp=count;


      //Copying the elements of the array into quotient array.


      for(i=0;i<count;i++)


      {quo[i]=B[i];}


      i=0;


      //Finding the binary equivalent of the digits by repetitive division.


      while(temp>0)


      {


            trav=0;rem=0;


            while(i<temp)


            {


                  num=quo[i]+(10*rem);


                  if(num<2 && i==0)


                  {


                        if(temp==1)


                        {


                              tru=1;


                              break;


                        }


                        num=(10*quo[i])+quo[i+1];


                        temp--;


                        for(k=1;k<temp;k++)


                        {


                              quo[k]=quo[k+1];


                        }


                  }


                  quo[i]=num/2;


                  rem=num%2;


                  i++;


                  trav++;


            }


            if(tru==1)


                  ans[j]=num;


            else


                  ans[j]=rem;


            dig++;


            j++;


            i=0;


            temp=trav;


      }


      printf("\nThe binary equivalent is:\n");  


      for(j=dig-1;j>=0;j--)


            printf("%d",ans[j]);


}

No comments:

Post a Comment