binary addition using class concept in c++

Post on 22-Jan-2018

40 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

/*

Program to print the sum of two binary numbers by taking two user inputs

*/

#include<iostream>

#include<stdlib.h>

using namespace std;

class binary

{

int a[4];

int b[4];

int c[5];

public:

void geta(int x,int y)

{

if(y!=0)

{

if(y!=1)

{

cout<<"wrong digit";

exit(0);

}

}

a[x]=y;

}

void getb(int u,int v)

{

if(v!=0)

{

if(v!=1)

{

cout<<"\n Wrong digit";

exit(1);

}

}

b[u]=v;

}

void add()

{

int i;

c[0]=0;

for(i=3;i>-1;i--)

c[i+1]=a[i]+b[i];

for(i=4;i>-1;i--)

{

if(c[i]==2)

{

c[i-1]=c[i-1]+1;

c[i]=0;

}

else if(c[i]==3)

{

c[i-1]=c[i-1]+1;

c[i]=1;

}

}

cout<<"\n The result is : "<<endl;

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

cout<<c[i];

}

};

main()

{

binary x;

int i,j,k;

cout<<"\n Enter the elements of 1st number ";

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

{

cin>>k;

x.geta(i,k);

}

cout<<"\n Enter the elements of 2nd number ";

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

{

cin>>k;

x.getb(i,k);

}

x.add();

}

top related