top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to substract the 2 numbers without using - or decrement operator?

0 votes
677 views
How to substract the 2 numbers without using - or decrement operator?
posted Sep 7, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
int main()
{
    int i =2,k= 6,z = 0;
    z = ((~k)+1) + i;

    printf("result  %d\n",z);
}
answer Sep 8, 2014 by Bheemappa G
I think this wont work for unsigned int.
So validate just replace these two and see the results.
unsigned int i =2,k= 6,z = 0;
printf("result  %u\n",z);

Thanks
Arshad Khan,
its works for all the cases,
In your case
 2 -6 = -4 (final result );
(-6 unsigned value is **********
-4 unsigned value is **********)
but here all are unsigned so -4 will be converted to unsigned .
0 votes

May not be the best way but try something like this

int main () 
{ 
    int a=5, b=1 ,c; 

    c=0;
    while (a >= b) // this is assuming that you are subtracting b from a and answer is positive
    { 
        b ++; c++; 
    } 
    printf("ans = %d \n",c); 
}
answer Sep 7, 2014 by Salil Agrawal
...