Download to read offline
![/*
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;
}](https://image.slidesharecdn.com/classbinaryadd-170514083312/75/Binary-addition-using-class-concept-in-c-1-2048.jpg)
![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();
}](https://image.slidesharecdn.com/classbinaryadd-170514083312/75/Binary-addition-using-class-concept-in-c-2-2048.jpg)

The document is a C++ program that sums two binary numbers based on user inputs. It defines a class to manage binary digits, validate inputs, and perform binary addition. The program reads two 4-bit binary numbers from the user, adds them, and displays the result.
![/*
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;
}](https://image.slidesharecdn.com/classbinaryadd-170514083312/75/Binary-addition-using-class-concept-in-c-1-2048.jpg)
![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();
}](https://image.slidesharecdn.com/classbinaryadd-170514083312/75/Binary-addition-using-class-concept-in-c-2-2048.jpg)