Solution for Exercise 8-14

ex0814

#include <stdio.h>

int main()
{
    int coordinate;

    printf("Input target coordinate: ");
    scanf("%d",&coordinate);
    if( coordinate >= -5 && coordinate <= 5 )
    {
        puts("Close enough!");
    }
    else
    {
        puts("Target is out of range!");
    }
    return(0);
}

Output

Input target coordinate: 8
Target is out of range!

Notes

* The comparison in Line 9 is inclusive because >= and <= are used. That means the range includes both -5 and 5. The exclusive equivalent would be:

* The else condition executes when variable coordinate is less than -5 or greater than 5. This is the opposite condition specified in Line 9.