#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
long num1,num2;
int first[10],second[10],ans[10],temp,size1=0,size2=0,sum1=0,sum2=0,size3=0;
clrscr();
cout<<"Enter 1st Binary Number :";
cin>>num1;
cout<<"Enter 2nd Binary Number :";
cin>>num2;
int i=0;
while(num1)
{
temp=num1%10;
first[i]=temp;
num1=num1/10;
i++;
size1++;
}
i=0;
while(num2)
{
temp=num2%10;
second[i]=temp;
num2=num2/10;
i++;
size2++;
}
for(i=0;i<size1;i++)
{
sum1+=((pow(2,i))*first[i]);
}
for(i=0;i<size2;i++)
{
sum2+=((pow(2,i))*second[i]);
}
i=0;
int total=sum1+sum2;
while(total)
{
temp=total%2;
ans[i]=temp;
total=total/2;
i++;
size3++;
}
cout<<endl<<endl<<endl;
for(i=size1-1;i>=0;i--)
{
cout<<" "<<first[i];
}
cout<<endl<<"+";
for(i=size2-1;i>=0;i--)
{
cout<<second[i]<<" ";
}
cout<<"\n============"<<endl;
for(i=size3-1;i>=0;i--)
{
cout<<ans[i]<<" ";
}
getch();
}
#include<conio.h>
#include<math.h>
void main()
{
long num1,num2;
int first[10],second[10],ans[10],temp,size1=0,size2=0,sum1=0,sum2=0,size3=0;
clrscr();
cout<<"Enter 1st Binary Number :";
cin>>num1;
cout<<"Enter 2nd Binary Number :";
cin>>num2;
int i=0;
while(num1)
{
temp=num1%10;
first[i]=temp;
num1=num1/10;
i++;
size1++;
}
i=0;
while(num2)
{
temp=num2%10;
second[i]=temp;
num2=num2/10;
i++;
size2++;
}
for(i=0;i<size1;i++)
{
sum1+=((pow(2,i))*first[i]);
}
for(i=0;i<size2;i++)
{
sum2+=((pow(2,i))*second[i]);
}
i=0;
int total=sum1+sum2;
while(total)
{
temp=total%2;
ans[i]=temp;
total=total/2;
i++;
size3++;
}
cout<<endl<<endl<<endl;
for(i=size1-1;i>=0;i--)
{
cout<<" "<<first[i];
}
cout<<endl<<"+";
for(i=size2-1;i>=0;i--)
{
cout<<second[i]<<" ";
}
cout<<"\n============"<<endl;
for(i=size3-1;i>=0;i--)
{
cout<<ans[i]<<" ";
}
getch();
}
No comments:
Post a Comment