424,962 Members | 1,998 Online
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

# comparing numbers

 P: 39 Hi.How can the greatest of three numbers be determined without using the comparison operator?Thanks for any help. Jerico Sep 20 '06 #1
5 Replies

 P: 61 Use the ternary operator (?:) Sep 20 '06 #2

 Expert Mod 5K+ P: 8,916 max(a, max(b,c)) Of course that just hides the comparision operator in the standard header files Sep 20 '06 #3

 P: 39 Hi Pukur. Could you show me how to use the ternary operator to get desired result?Thanks. Jerico Sep 20 '06 #4

 P: 61 (condition)? e1:e2; If the condition is true then e1 will be evaluated otherwise e2 will be evaluated. Let us suppose that a,b,c are the three numbers and the statement for finding the maximum of these three is..... max=(a>b)?((a>c)?a:c):((b>c)?b:c); Sep 20 '06 #5

 P: 27 hi fellows, jerico wondered how to get greatest of 3 numbers without using the comparison operator and I actually see three of them in this pukur123's statement: max=(a>b)?((a>c)?a:c): ((b>c)?b:c); well, here is my solution (works for unsigned integer values): Expand|Select|Wrap|Line Numbers int getMax(unsigned a, unsigned b, unsigned c) {     int m=0;     while((a?a--:0)+(b?b--:0)+(c?c--:0)) m++;     return m; } Sep 20 '06 #6