// Program by Akash Tripathi (@proakash256)
// In This Method :
// We add the negative of second number
// to the first number
// ^ XOR Operator is used to add the Bits
// & AND Operator is used to get the Carry
// << LEFT SHIFT is used to shift the Carry
// One Position left so that it can be added
// to the next bit.
#include <stdio.h>
int main()
{
int a, b;
printf("Enter the first number : ");
scanf("%d", &a);
printf("\nEnter the second number : ");
scanf("%d", &b);
int c = a;
int d = (~b + 1); // Getting the negative
// of Second Number
int difference = 0;
int carry = 0;
while(d != 0)
{
difference = c ^ d; // Adding both numbers
carry = c & d; // Finding the Carry
c = difference;
d = carry << 1; // Left Shifting the carry
}
printf("\nBy Second Method :");
printf("\n%d - %d = %d \n\n", a, b, difference);
return 0;
}
Comments
Post a Comment