C program for addition of binary numbers

C code for sum of two binary numbers:  


int main(){

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

    printf("Enter any first binary number: ");
    printf("Enter any second binary number: ");

         sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
         remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
         binary1 = binary1/10;
         binary2 = binary2/10;

         sum[i++] = remainder;

    printf("Sum of two binary numbers: ");

   return 0;

Sample output:

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


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.


NSR said...

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

Anonymous said...


this can be efficiently written as

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:


int main(){

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

printf("Enter any first octel number: ");
printf("Enter any second octel number: ");

sum[i++] = (octel1 %10 + octel2 %10 + remainder ) %8;
remainder = (octel1 %10 + octel2 %10 + remainder ) / 8;
octel1 = octel1/10;
octel2 = octel2/10;

sum[i++] = remainder;

printf("Sum of two octel numbers: ");
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;}
if((na==1)&& (nb==1) && (ci==1)){
s=1; ci=1;}
if (((na==0)&&(nb==0)&&(ci==1))||
if (((na==0)&&(nb==1)&&(ci==1))||
{s=0; ci=1;}
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