Tuesday, 18 October 2016

10binaryadditionc2_1476775079730

/*write c++ program using STL to add binary numbers(assume one bit as one number);use STL stack*/
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int i;
stack<int> A;
stack<int> B;
stack<int> C;
char First_number[5],Second_number[5];
int a=0,b=0;
cout<<"\n Enter the First binary number";
cin>>First_number;
for(i=0;First_number[i]!='\0';i++)
{
if(First_number[i]=='0')
A.push(0);
else
A.push(1);
}
//A stack stores first binary number
cout<<"\nEnter the Second binary number";
cin>>Second_number;
for(i=0;Second_number[i]!='\0';i++)
{
if(Second_number[i]=='0')
B.push(0);
else
B.push(1);
}
//B satck stores second binary number
int carry=0;
while(!A.empty()||!B.empty())
{
a=0;b=0;
if(!A.empty())//length of first number is larger than second
{
a=A.top();
A.pop();
}
if(!B.empty())//length of second number is larger than first
{
b=B.top();
B.pop();
}
int sum=carry+a+b;//performing addition
C.push(sum%2);//pushing the result onto the third stack
carry=sum/2;//setting carry
}
if(carry==1)//if carry is there then
C.push(carry);//it is pushed at the end onto the stack
cout<<"\nAddition of two binary numbers is...";
while(!C.empty())//C stack is popped to display result
{
cout<<C.top();
C.pop();
}
cout<<"\n";
return 0;
}



/*output
[student@localhost ~]$ g++ assignment10.cpp
[student@localhost ~]$ ./a.out

 Enter the First binary number1101

Enter the Second binary number1100

Addition of two binary numbers is...11001 */



No comments:

Post a Comment