How To Find Max Of 3 Numbers Without Using Comparison Operators?

rasheed1982
0
Newbie Poster

## Recommended Answers

Jump to Postin c++ create a vector<int>, sort it, then print the 3d number. Otherwise I have no clue how to do it without comparison operator.

`vector<int> array; array.resize(3); // populate the array with 3 random values array[0] = rand(); array[1] = rand(); array[3] = rand(); // sort sort(array.begin(),array.end()); …`

Jump to Postsimple arithmetic is all you need...

max(a,b) = ( a + b + abs ( a - b ) ) / 2

min(a,b) = ( a + b - abs ( a - b ) ) / 2

Jump to PostIts no coding secret or something but a simple formula kind of thing here it is :

For

twonumbers:

max( a , b ) = ( a + b + abs( a - b ) ) / 2For

threenumbers it is:

max( a …

## All 12 Replies

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

rasheed1982
0
Newbie Poster

SpS
34
Posting Pro

Rashakil Fol
978
Super Senior Demiposter
Team Colleague

SpS
34
Posting Pro

Narue
5,707
Bad Cop
Team Colleague

Bench
212
Posting Pro

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

Narue
5,707
Bad Cop
Team Colleague

SpS
34
Posting Pro

6finger
1
Newbie Poster

dkalita
commented:
great :)
+1

csurfer
422
Posting Pro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.