#include<iostream>
using namespace std;
int addBinary(int a,int b);/* declaring function to add the binary numbers*/
int reverse(int n);/*function to reverse the number*/
int main(){
/*declaring two variables a and b to take two numbers as input*/
int a,b;
cout<<"Enter the value of the 1st Binary number";
cin>>a;
cout<<"Enter the value of the 2nd Binary number";
cin>>b;
/* printing the value returned by the function that is result or sum of the 2 binary numbers*/
cout<< addBinary(a,b)<<endl;
}
int addBinary(int a,int b){
/* variable to store the sum and initialising its value to zero*/
int ans=1;
/*variable to store the previous carry and initialising its value to zero*/
int prevCarry=0;
while(a>0 && b>0){
/* coding the 3 cases learned above*/
if(a%2==0 && b%2==0){
ans=ans*10+prevCarry;
/*initialising prevCarry=0 because there is no prevCarry generated above*/
prevCarry=0;
}
else if((a%2==0 && b%2==1) || (a%2==1 && b%2==0)){
/*firstly we need to check if prevCarry was 0 or 1*/
if(prevCarry==1){
ans=ans*10+0;
prevCarry=1;
}
else{
ans=ans*10+1;
prevCarry=0;
}
}
else /* if both the numbers are 1*/
{
ans=ans*10+prevCarry;
prevCarry=1; /*since 1+1 always generates a carry 1*/
}
/* updating a and b values*/
a/=10;
b/=10;
}
/* if one of the bnumbers a or b has been finished traversing that is the input numbers a and b are not of same lenght then we need to take care of the other number and add it with the carry with the left out numbers*/
while(a>0){
if(prevCarry==1){
/* we need to check whether the number to which we are adding the prevCarry is 1 or not*/
if(a%2==1){
ans=ans*10 + 0;
prevCarry=1;/*since 1+1 generates a carry*/
}
else{
ans=ans*10 + 1;
prevCarry=0;/*initialising it to zero since the prevCarry has been utilized above*/
}
}
/* if prevCarry==0*/
else{
ans=ans*10 + (a%2);
}
/*updating value of a*/
a/=10;
}
/* same goes for b as well if b has been leftout */
while(b>0){
if(prevCarry==1){
/* we need to check whether the number to which we are adding the prevCarry is 1 or not*/
if(b%2==1){
ans=ans*10 + 0;
prevCarry=1;/*since 1+1 generates a carry*/
}
else{
ans=ans*10 + 1;
prevCarry=0;/*initialising it to zero since the prevCarry has been utilized above*/
}
}
/* if prevCarry==0*/
else{
ans=ans*10 + (b%2);
}
/*updating value of b*/
b/=10;
}
/* if and b values are of same length that is input numbers are equal then atlast if we are leftout with a carry*/
if(prevCarry==1){
/* adding the prevCarry to the final answer*/
ans= ans*10+1;
}
/* we need to reverse the result because we are adding the numbers here since we are adding numbers by using the %10 logic the first number will become the last number so we need to reverse the number*/
/*function to reverse the number*/
ans =reverse(ans);
return ans;
}
int reverse(int n){/*declaring n as a parameter to store the value of the answer from the above function*/
int ans=0;/* declaring variable to store the reverse number and initialising it to 0 */
while(n>0){
int lastnum=n%10;
ans=ans*10+lastnum;
n/=10;/*updating n value*/
}
/*returning value to the above function where reverse function has been called*/
ans=ans/10 ;
return ans;
}
Comments