I am trying to write a program that adds any pair of binary numbers up to 31 digits correctly.
I am having a very difficult time with this. I can't figure out how to let it accept 31 digits, how to add two numbers that are not the same amount of digits like 1001 and 101, and I can't get it to work with getch().
Any help would be great!
Code:
#include<stdio.h>
#include<curses.h>
int main()
{
int a[8],b[8],c[9], carry=0;
int i,j;
//int sum;
printf("Enter the First Eight bit number: ");
for(i=0;i<8;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Second Eight bit number: ");
for(i=0;i<8;i++)
{
scanf("%d",&b[i]);
}
j=8;
for(i=7;i>=0;i--)
{
if (a[i]==1 && b[i]==1 && carry ==1)
{
c[j] = 1;
carry =1;
}
else if (a[i]==1 && b[i]==1 && carry ==0)
{
c[j]=0;
carry=1;
}
else if (a[i]==0 && b[i]==1 && carry ==0)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==1 && carry ==1)
{
c[j]=0;
carry=1;
}
else if (a[i]==1 && b[i]==0 && carry ==1)
{
c[j]=0;
carry=1;
}
else if (a[i]==1 && b[i]==0 && carry ==0)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==0 && carry ==1)
{
c[j]=1;
carry=0;
}
else if (a[i]==0 && b[i]==0 && carry ==0)
{
c[j]=0;
carry=0;
}
j--;
}
c[0] = carry;
printf("\nthe sum of the two binary number is: ");
for(j=0;j<=8;j++)
{
printf("%d",c[j]);
}
getch();
return 0;
}