Let us learn how to subtract two numbers without using subtraction (–) operator in C programming language.
We generally use the minus operator (–) to subtract one number from another. Here, we shall use the postfix decrement operator to find the product of the numbers. Alternatively, two numbers can be subtracted using Bitwise Operators.
Alternatively, two numbers can be subtracted using Bitwise Operators.
The first code to find the subtraction of two numbers without using subtraction operator (–) makes use of the postfix (—) operator. If you want to subtract two integers without arithmetic operators, then you will have to use the
If you want to subtract two integers without arithmetic operators, then you will have to use the bitwise operators which is demonstrated in the second program by using Bitwise Shift Operators.
C Program To Subtract Two Numbers without using Subtraction Operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> int main() { int a, b, count = 0, temp; printf("\nEnter First Number:\t"); scanf("%d", &a); printf("\nEnter Second Number:\t"); scanf("%d", &b); temp = a; while(count < b) { a--; count++; } printf("\nSubtraction of Two Numbers %d and %d:\t%d\n", temp, b, a); return 0; } |
C Program To Subtract Two Numbers using Bitwise Operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { int a, b, num1, num2; printf("\nEnter First Number:\t"); scanf("%d", &a); printf("\nEnter Second Number:\t"); scanf("%d", &b); num1 = a; num2 = b; while (b != 0) { int borrow = (~a) & b; a = a ^ b; b = borrow << 1; } printf("\nSubtraction of Two Numbers %d and %d:\t%d\n", num1, num2, a); return 0; } |
Output

If you have any compilation error or doubts in this C program to subtract two integers using Bitwise Operators, let us know about it in the comment section below.