title: Using only 4 'if' statements - Find Largest and Smallest of 4 integers
body: <p>I have been going through some basic exercises from a recommended beginner's book: <a href="http://knking.com/books/c2/index.html" rel="nofollow noreferrer">C Programming: A Modern Approach (2nd Edition) </a> </p>
  
  <p>The question states: Use as few if statements as possible to determine the largest and smallest of four numbers entered by the user. <strong>Four if statements are sufficient.</strong> -- Since this question was asked before loops, arrays and functions were covered in the book, I am assuming that these should not be used. </p>
  
  <p>Also, I know there was a similar <a href="https://stackoverflow.com/questions/19199473/biggest-and-smallest-of-four-integers-no-arrays-no-functions-fewest-if-stat">question</a> to this one, however none meet the requirements of what I am trying to achieve.</p>
  
  <ol>
  <li>Using <strong>only 4</strong> <code>if</code> statements</li>
  <li>No for-loops.</li>
  </ol>
  
  <p>The first thing that came to my mind was using the logical <code>or</code> operator however as shown below, eight <code>if</code> statements were used. Also this method is very long and not efficient:</p>
  
  <pre><code>    int a, b, c, d;
  
  printf("Enter 4 intgeres to find largest and smallest: ");
  scanf_s("%d %d %d %d", &amp;a, &amp;b, &amp;c, &amp;d);
  
  if (a &gt; b &amp;&amp; a &gt; c &amp;&amp; a &gt; d)
      printf("Largest: %d\n", a);
  if (b &gt; a &amp;&amp; b &gt; c &amp;&amp; b &gt; d)
      printf("Largest: %d\n", b);
  if (c &gt; a &amp;&amp; c &gt; b &amp;&amp; c &gt; d)
      printf("Largest: %d\n", c);
  if (d &gt; a &amp;&amp; d &gt; a &amp;&amp; d &gt; c)
      printf("Largest: %d\n", d);
  
  if (a &lt; b &amp;&amp; a &lt; c &amp;&amp; a &lt; d)
      printf("Smallest: %d\n", a);
  if (b &lt; a &amp;&amp; b &lt; c &amp;&amp; b &lt; d)
      printf("Smallest: %d\n", b);
  if (c &lt; a &amp;&amp; c &lt; b &amp;&amp; c &lt; d)
      printf("Smallest: %d\n", c);
  if (d &lt; a &amp;&amp; d &lt; a &amp;&amp; d &lt; c)
      printf("Smallest: %d\n", d);
  
  return 0;
  </code></pre>
  
  <p>Next, I proceeded to the following code which would be a better solution:</p>
  
  <pre><code>    int a, b, c, d;
  
  printf("Enter 4 intgeres to find largest and smallest: ");
  scanf_s("%d %d %d %d", &amp;a, &amp;b, &amp;c, &amp;d);
  
  int max = a, min = a;
  
  if (b &gt; max) 
      max = b;
  else if (b &lt; min) 
      min = b;
  if (c &gt; max) 
      max = c;
  else if (c &lt; min) 
      min = c;
  if (d &gt; max) 
      max = d;
  else if (d &lt; min) 
      min = d;
  
  printf("max: %d min : %d\n", max, min);
  
  return 0;
  </code></pre>
  
  <p>However, still not meeting the requirement of using 4 <code>if</code> statements. I was wondering if I could shorten my code even further. Please excuse the basic nature of this question. Any suggestions would be appreciated.</p>