C program for addition of binary numbers







C code for sum of two binary numbers:  


#include
<stdio.h>

int main(){

    long int binary1,binary2;
    int i=0,remainder = 0,sum[20];

    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any 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;
}

Sample output:

Enter any first binary number: 1100011
Enter any second binary number: 1101
Sum of two binary numbers: 1110000


Alogrithm:

Rule of binary addition:
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 1 and carry = 1

Q1. What is the sum of the binary numbers 1101 and 1110?
Answer: 1101 + 1110 = 11011





6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

16 comments:

NSR said...

i want examples of programs using bitwise operators..(like left shift,right shift etc)

Anonymous said...

while(binary1!=0||binary2!=0)

this can be efficiently written as
while(binary1||binary2)

Also what happens if the numbers are negative ?
Where is the validation for the given number is binary or not.

Anonymous said...

please help me with c or c++ program that can do addition of two number in octel notation,that is base8.



there is my code:

#include
#include

int main(){

long int octel1,octel2;
int i=0,remainder = 0,sum[20];

printf("Enter any first octel number: ");
scanf("%ld",&octel1);
printf("Enter any second octel number: ");
scanf("%ld",&octel2);

while(octel1!=0||octel2!=0){
sum[i++] = (octel1 %10 + octel2 %10 + remainder ) %8;
remainder = (octel1 %10 + octel2 %10 + remainder ) / 8;
octel1 = octel1/10;
octel2 = octel2/10;
}

if(remainder!=0)
sum[i++] = remainder;

--i;
printf("Sum of two octel numbers: ");
while(i>=0)
printf("%d",sum[i--]);
system("pause");
return 0;
}

Anonymous said...

it is very urgent. thanks in advance

settimo vittorio said...
This comment has been removed by the author.
settimo vittorio said...

you have to delete one #include, then make the other #include an #include stdio.h, and (sorry for my bad english) less than and greater than symbols must be put before and after the stdio.h word, I don't know why but i can't use them here.

mora said...

void adder(int a,int b,int ci){

int na,nb,s;
if ((a==0) && (b==0)){printf("%d ",ci); return;}
na=a%10;
nb=b%10;
if((na==1)&& (nb==1) && (ci==1)){
s=1; ci=1;}
if (((na==0)&&(nb==0)&&(ci==1))||
((na==0)&&(nb==1)&&(ci==0))||
((na==1)&&(nb==0)&&(ci==0))){
s=1;
ci=0;}
if (((na==0)&&(nb==1)&&(ci==1))||
((na==1)&&(nb==0)&&(ci==1))||
((na==1)&&(nb==1)&&(ci==0)))
{s=0; ci=1;}
adder(a/10,b/10,ci);
printf("%d ",s);


}

mittu uma said...

Rule of binary addition:

1 + 1 = 0 (not 1 as u mentioned) and carry = 1

Manirujjaman Akash said...

can I do it without using array /
?

sushan shrestha said...

can you reply c code for subtraction of 2 unsigned integer binary number.... thnx in adv :D

ghassen lassoued said...

why the --i ???

HARI BITTU said...

Can u please help me how to write a program to find the carry, sum, and checksum of 4 16-bit binary numbers. The input and
output will be in binary format. Please do error checking for non-binary number.

Ansuman Ghosh said...

try to add 1, and 10 ... ;) it will not work

Unknown said...
This comment has been removed by the author.
Durgesh said...
This comment has been removed by the author.
Durgesh said...

It can be solved without using array with the help of recursion.
Also the above code won't work if both the inputs are '0'
For ex: '000000' , '0000'

#include
void add(long int bin1,long int bin2,int remainder)
{
int sum;
if(bin1==0&&bin2==0)
{
printf("%d ",bin1);
return;
}
sum=(bin1%10 +bin2%10 +remainder)%2;
remainder=(bin1%10 +bin2%10 +remainder)/2;
bin1/=10;
bin2/=10;
add(bin1,bin2,remainder);
if(remainder!=0&&bin1==0&&bin2==0)
printf("%d ",remainder);
printf("%d ",sum);
}
int main()
{
long int bin1,bin2;
int i=0,j=0;
scanf("%ld %ld",&bin1,&bin2);
add(bin1,bin2,0);
return 0;
}