Loading...

Find sum of two Binary Numbers

Programme:

#include <stdio.h>

int main()
{
    long binary1, binary2;
    int i =0, remainder =0, sum[20];
    
    printf("Enter the first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter the second binary number: ");
    scanf("%ld",&binary2);
    while(binary1 !=0|| binary2 !=0)
    {        
        sum[i++]=(binary1 %10+ binary2 %10+ remainder)%2;
        remainder =(binary1 %10+ binary2 %10+ remainder)/2;        
        binary1 = binary1 /10;        
        binary2 = binary2 /10;
    }
    if(remainder !=0)        
    sum[i++]= remainder;
    --i;
    printf("Sum of two binary numbers: ");
    while(i >=0)
    printf("%d", sum[i--]);
    return 0;
}

Output:

Enter the first binary number: 101011                                                                                       
Enter the second binary number: 1010111                                                                                     
Sum of two binary numbers: 10000010