write a program to compare 2 numbers without using logical
operators?

Answer Posted / rishabh

#include<stdio.h>
#include<limits.h>
int sign(int number)
{
return (unsigned) number / (unsigned) INT_MIN;
}
int main(int argc, char *argv[])
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int dif = a - b;
int sb1 = sign(dif);
int sb2 = sign(dif - 1) - sb1;
int ptr = 2 * sb2 + sb1;
char *messages[3] =
{
"%d is greater than %d",
"%d is less than %d",
"%d is equal to %d" };
printf(messages[ptr], a, b);
}
}

Is This Answer Correct ?    3 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between functions abs() and fabs()?

641


What is a good way to implement complex numbers in c?

588


a program that can input number of records and can view it again the record

1476


What are the scope of static variables?

597


Explain how can you avoid including a header more than once?

593






Can you please explain the difference between exit() and _exit() function?

585


What does dm mean sexually?

806


Which type of language is c?

642


Why do some versions of toupper act strangely if given an upper-case letter?

627


What is a built-in function in C?

785


What is I ++ in c programming?

617


What is output redirection?

684


What are the types of pointers in c?

526


How to establish connection with oracle database software from c language?

1669


What is function prototype in c with example?

570