forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBinary_num.cpp
63 lines (62 loc) · 1.05 KB
/
AddBinary_num.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<stack>
using namespace std;
stack<int> read()
{
char a[40];
stack<int> s;
cout<<"\n Enter a binary number: ";
cin>>a;
for(int i=0;a[i]!='\0';i++)
{
if(a[i]=='1')
s.push(1);
else
if(a[i]=='0')
s.push(0);
}
return s;
}
stack<int> add(stack<int> &s1,stack<int> &s2)
{
stack<int> s;
int sum,carry=0,bit1,bit2;
while(!s1.empty()||!s2.empty())
{
bit1=bit2=0;
if(!s1.empty())
{
bit1=s1.top();
s1.pop();
}
if(!s2.empty())
{
bit2=s2.top();
s2.pop();
}
sum=(bit1+bit2+carry)%2;
carry=(bit1+bit2+carry)/2;
s.push(sum);
}
if(carry==1)
s.push(1);
return s;
}
void display(stack<int> &s)
{
cout<<"\n Sum= ";
while(!s.empty())
{
cout<<s.top();
s.pop();
}
}
int main()
{
stack<int> s1,s2,s3;
s1=read();
s2=read();
s3=add(s1,s2);
display(s3);
return 0;
}