Sunday, October 18, 2009

Binary to Decimal

#include<iostream>
#include<cmath>

using namespace std;


class Binary
{
      int a[50],size,i,deg;
     
      public:void setdata(void);
             void cal(void);
};

void Binary::setdata(void)
{
     cout<<"enter size of the binary no.:";
     cin>>size;
     if(size<=8)
     {
                cout<<"enter the binary no.";
                for(i=0;i<=size-1;i++)
                cin>>a[i];
                }
                else{
                     cout<<"invalid input";
                     }
                     }
                     void Binary::cal(void)
                     {
                      double s=0;
                      deg=0;
                      for(i=size-1;i>=0;i--)
                      {
                                 s=s+a[i]*pow(2.0,deg);
                                 deg++;
                                 }
                       cout<<"\n\nthe decimal number is:"<<s;
                       }


int main()
{
    int choice;
     Binary b;
     L1:b.setdata();
     b.cal();
     if(choice==1)
     goto L1;
   return 0;
     }  
            

No comments:

Post a Comment