Thursday, December 31, 2015

C program to add 2 numbers without using arithmetic operators

#include <stdio.h>
int main(void) {
  int a = 3, b = 5, c = 0;
  while(b != 0){
    c = a & b; // carry
    a = a ^ b;
    b = c << 1;
  }
  printf("%d",a);
  return 0;
}

Ref: http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/

No comments:

Post a Comment