Check if 2 rectangles fit in another one, given their length and height I was very intrigued with the manner to solve this problem. The program needs to know if 2 rectangles fit in another one, considering the lines of the 2 rectangles are always parallel to the other one's.

I know I could just put everything in a single if, but is that more readable? I want to know a good approach for this situation, using only conditions.

NOTE: x and y represent the first rectangle's width and height (the one that will fit the other 2) and l1, h1, l2, and h2 represent the other 2 rectangles' width and height.

#include &lt;stdio.h&gt;                                                                                  

int main(void)                                                                                      
{                                                                                                                                         
    int x, y, l1, h1, l2, h2;                                                                       

    scanf("%d %d %d %d %d %d", &amp;x, &amp;y, &amp;l1, &amp;h1, &amp;l2, &amp;h2);                                         

    if ((l1 + l2 &lt;= x &amp;&amp; h1 &lt;= y &amp;&amp; h2 &lt;= y) || 
    (h1 + h2 &lt;= x &amp;&amp; l1 &lt;= y &amp;&amp; l2 &lt;= y))               
    {                                                                                               
        puts("They fit.");                                                                                                                                                           
    }                                                                                                                       
    else if ((l1 &lt;= x &amp;&amp; h2 &lt;= x &amp;&amp; h1 + l2 &lt;= y) || 
    (h1 &lt;= x &amp;&amp; l2 &lt;= x &amp;&amp; l1 + h2 &lt;= y))          
    {                                                                                               
        puts("They fit.");                                                                                                                                                             
    }                                                                                                                        
    else if ((l1 &lt;= y &amp;&amp; h2 &lt;= y &amp;&amp; h1 + l2 &lt;= x) || 
    (h1 &lt;= y &amp;&amp; l2 &lt;= y &amp;&amp; l1 + h2 &lt;= x))          
    {                                                                                               
        puts("They fit.");                                                                                                                                                             
    }                                                                                                                       
    else if ((l1 + l2 &lt;= y &amp;&amp; h1 &lt;= x &amp;&amp; h2 &lt;= x) || 
    (h1 + h2 &lt;= y &amp;&amp; l1 &lt;= x &amp;&amp; l2 &lt;= x))          
    {                                                                                               
        puts("They fit.");                                                                                                                                                          
    }                                                                                                                                                               
    else                                                                                            
    {                                                                                               
        puts("They don't fit.");                                                                                                                                                           
    }   
    return 0;
}
Any two rectangles can fit into a third rectangle (without overlapping with each other) only under 2 cases:

As mentioned in the question, consider:


x,y  -> length and height of first rectangle into which below two rectangles must fit.
l1,h1 -> length/width and height of second rectangle.
l2,h2 -> length and height of third rectangle.


Note: You could use structures or 2-dimensional arrays for better readability.



Case 1:


When x is greater than or equal to l1+l2 
Here the height y must be greater than or equal to maximum of h1 and h2 for the rectangles to fit in, else it doesn't fit in.




Note: x &gt;= l1+12 can be written as ((x &gt;= l1) &amp;&amp; (x-l1 &gt;= l2)). This is to avoid the overflow while adding integers. 



Case 2:


When y is greater than or equal to h1+h2
Here the length x must be greater than or equal to maximum of l1 and l2 for the rectangles to fit in, else it doesn't fit in.




Note: similarly, even here y &gt;= h1+h2 can be written as (y &gt;= h1) &amp;&amp; (y-h1 &gt;= h2). This is to avoid the overflow while adding integers. 



Now, keeping the above 2 cases in mind, a simple function can be constructed this way:

int fit( int x, int y, int l1, int h1, int l2, int h2)
{
    //return 1 for fit and 0 if does not fit

    //case 1 or case 2
    if( ( ((x &gt;= l1) &amp;&amp; (x-l1 &gt;= l2)) &amp;&amp; y &gt;= max(h1,h2)) || 
        ( ((y &gt;= h1) &amp;&amp; (y-h1 &gt;= h2)) &amp;&amp; x &gt;= max(l1,l2)) )
    {
            return 1;
    }

    return 0;
}


Note: here max() is a function which returns the larger value among the two sent arguments.

int max(int a, int b)
{
    if(a&gt;b)
    {
        return a;
    }
    return b;
}




Now, the 2 rectangles can be sent in 4 different ways to check if they can fit in the rectangle of dimensions x and y:

//sending as entered by the user
fit( x, y, l1, h1, l2, h2)

//interchanging length and height of anyone rectangle
fit( x, y, h1, l1, l2, h2)

//interchanging length and height of other rectangle
fit( x, y, l1, h1, h2, l2)

//interchanging lengths and heights of both rectangles
fit( x, y, h1, l1, h2, l2)





Now, you can easily know whether the rectangles fit or does not fit by checking for the return value of fit() function. 
If return value of anyone of the above four function calls is 1 then the rectangles fit in!
You could use the main() function as:

int main(void)
{
    int x, y, l1, h1, l2, h2;

    scanf("%d %d %d %d %d %d", &amp;x, &amp;y, &amp;l1, &amp;h1, &amp;l2, &amp;h2);

    //tests for fit
    if(fit( x, y, l1, h1, l2, h2) || 
       fit( x, y, h1, l1, l2, h2) || 
       fit( x, y, l1, h1, h2, l2) || 
       fit( x, y, h1, l1, h2, l2))
    {
        printf("fits");
    }
    else
    {
        printf("doesn't fit");
    }

}

The above if block functions similarly as an else-if ladder because, || stops evaluation on the first true (i.e, function returns 1).
So, if any of the function returns a 1, then then "fits" gets printed out and program terminates
In case, if none of the function calls return 1 the "doesn't fit" gets printed out and program terminates




Altogether your code would be:

#include &lt;stdio.h&gt;

int max(int a, int b)
{
    if(a&gt;b)
    {
        return a;
    }
    return b;
}

int fit( int x, int y, int l1, int h1, int l2, int h2)
{
    //return 1 for fit and 0 if does not fit

    //case 1 or case 2
    if( ( ((x &gt;= l1) &amp;&amp; (x-l1 &gt;= l2)) &amp;&amp; y &gt;= max(h1,h2)) || 
        ( ((y &gt;= h1) &amp;&amp; (y-h1 &gt;= h2)) &amp;&amp; x &gt;= max(l1,l2)) )
    {
            return 1;
    }

    return 0;
}



int main(void)
{
    int x, y, l1, h1, l2, h2;

    scanf("%d %d %d %d %d %d", &amp;x, &amp;y, &amp;l1, &amp;h1, &amp;l2, &amp;h2);

    //tests for fit
    if( fit( x, y, l1, h1, l2, h2) || 
        fit( x, y, h1, l1, l2, h2) || 
        fit( x, y, l1, h1, h2, l2) || 
        fit( x, y, h1, l1, h2, l2))
    {
        printf("fits");
    }
    else
    {
        printf("doesn't fit");
    }

}