**Logic of a Program**

To subtract two numbers here we are using

**2's complement**. To understand the logic let's take two number 4 and 3.

First calculate the 2's Complement of 3.

Binary representation of 3 is

**0011**

**Now 1's complement can be obtained by**

**inverting all the bits of binary representation**of 3. And by adding 1 in 1's complement we get 2's complement.

1's complement of 3 is

**1100**.

2's complement of 3 is

**1101**. ( By adding 1 in 1's complement we get 2's complement).

Let's add 4 and 2's complement of 3.

0100 + 1101 = 0001 ( This is the binary representation of 1).

Tricky Problems

Add Two Numbers Without Using Addition Operator

### Write a Program to Subtract Two Numbers Without Using Subtraction (-) Operator

#include<stdio.h> int main(){ int firstnum,secondnum,result,complement; printf("Enter any two numbers "); scanf("%d%d",&firstnum,&secondnum); // Take two's complement of a number complement = ~secondnum +1; // Add firstnum with two's complement result = firstnum + complement; // Final result printf("Difference of two integers: %d",result); return 0; }

## No comments:

## Post a Comment