/*Serial No.50 [swami22.cpp]*/
/*Assumption --> in overflow condition the carry bit is discarded because sum is stored in array of 8 elements*/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int a[8];
int b[8];
int sum[8],carry=0,i,j,temp;
clrscr();
printf("while entering digits from left to write press ENTER after each digit.\n");
printf("Enter 1st 8 bit Binary number\n");
for(i=0;i<8;i++)
{
scanf("%d",&temp);
if(temp==0 || temp==1)
a[i]=temp;
else
{
printf("Wrong Entry!");
getch();
exit(1);
}
}
printf("\n\nEnter 2nd 8 bit binary number\n");
for(i=0;i<8;i++)
{
scanf("%d",&temp);
if(temp==0 || temp==1)
b[i]=temp;
else
{
printf("Wrong Entry!");
getch();
exit(1);
}
}
for(i=7;i>=0;i--)
{
if(a[i]==0&&b[i]==0&&carry==0)
{sum[i]=0;carry=0;}
else if(a[i]==0&&b[i]==0&&carry==1)
{sum[i]=1;carry=0;}
else if(a[i]==0&&b[i]==1&&carry==0)
{sum[i]=1;carry=0;}
else if(a[i]==0&&b[i]==1&&carry==1)
{sum[i]=0;carry=1;}
else if(a[i]==1&&b[i]==0&&carry==0)
{sum[i]=1;carry=0;}
else if(a[i]==1&&b[i]==0&&carry==1)
{sum[i]=0;carry=1;}
else if(a[i]==1&&b[i]==1&&carry==0)
{sum[i]=0;carry=1;}
else if(a[i]==1&&b[i]==1&&carry==1)
{sum[i]=1;carry=1;}
}
printf("\nsum=");
for(i=0;i<8;i++)
{
printf("%d",sum[i]);
}
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...