Addition of Binary

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<iostream>

using namespace std;

int main()
{
long num1, num2;
int i=0, rem=0, sum[20];

cout<<"-----Addition of Binary-----"<<endl;
cout<<endl;
cout<<"Enter 1st binary number(s): ";
cin>>num1;
cout<<endl;
cout<<"Enter 2nd binary number(s): ";
cin>>num2;
cout<<"------------------------------------------";
cout<<endl;
while (num1 !=0 || num2 !=0)
{
sum[i++] = (int)((num1 % 10 + num2 % 10 + rem) % 2);
rem = (int)((num1 % 10 + num2 % 10 + rem) / 2);
num1 = num1 / 10;
num2 = num2 / 10;
}
if (rem!=0)
{
sum[i++] = rem;
}
i--;
cout<<"The sum of two binary number is: ";
while (i>=0)
{
cout<<(sum[i--]);
}
cout<<(endl);
cout<<"------------------------------------------"<<endl;

system("pause");
return 0;
}

You might also like