Addition of two numbers without using the Addition Operator

  • The first technique uses the notion of negative multiplied by minus to get a positive result. a-(-b) becomes a+b in this case. 
  • As a result, the screen shows the sum of variables a and b. The similar principle is applied in the second technique. The third technique use the predefined function abs().
  • The stdlib.h header file defines the abs() function. It returns a number's absolute value. The bitwise complement operator is utilised in the fourth approach. The integer N's bitwise complement is equal to -(N+1). As a result, a-(b)-1 equals a-(-(b+1)). a+b+1-1 => a+b+1-1 => a+b+1-1 => a+b+1-1 => a+b+1-1 =>

Program for Addition of two numbers without using the Addition Operator
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
  int x, y;
  printf("Enter two number: ");
  scanf("%d %d",&x,&y);
  // method 1
  printf("%d\n", x-(-y));
  // method 2
  printf("%d\n", -(-x-y));
  // method 3
  printf("%d\n", abs(-x-y));
  // method 4
  printf("%d", x-(~y)-1);
 getch();
}
Expected Output:-
Enter two number: 25
35
60

Post a Comment

Previous Post Next Post