C Programming

C Program Addition and Subtraction without using + – Operators

We can do addition and subtraction without using +,- operators in C Language.

C Program Addition :

Addition.c
#include<stdio.h>
 int addition(int x, int y) {
   int a, b;
   do {
     a = x & y;
     b = x ^ y;
     x = a << 1;
     y = b;
   } while (a);
   return b;
 }
 int main(void) {
   printf("10+20 = %d", addition(10, 20));
   return 0;
 }

Output:

Terminal
10+20 = 30

C Program Subtration :

Sub.c
#include<stdio.h> 

int sub(int x, int y) {
  unsigned a, b;
  do {
    a = ~x & y;
    b = x ^ y;
    x = b;
    y = a << 1;
  } while (a);
  return b;
}
int main(void) {
  printf("10 - 20 = %d", sub(10, 20));
  return 0;
}

Output:

Terminal
10 - 20 = -10

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

Ask a question or leave a comment