# Simple Conditions – Problems

The second problem of the "Programming Basics" Practical Exam covers conditional statement and simple calculations. Let's take a few examples.

## Problem: Point on Segment

A horizontal segment is set on a horizontal line, set with the x coordinates of both ends: first and second. A point is located on the same horizontal line and is set with its x coordinate. Write a program that checks whether the point is inside or outside the segment and calculates the distance to the nearest end of the segment.

### Input

The console reads 3 integer numbers (one per line):

• On the first line stands the number first - one end of the segment.
• On the second line stands the number second – the other end of the segment.
• On the third line stands the number point – the location of the point.

All input numbers are integers in the range [-1000 … 1000].

### Output

Print the result on the console:

• On the first line print "in" or "out" - whether the point is inside or outside the segment.
• On the second line print the distance from the point to the nearest end of the segment.

### Sample Input and Output

Input Output Visualization
10
5
7
in
2
Input Output Visualization
8
10
5
out
3
Input Output Visualization
1
-2
3
out
2

We read the input from the console.

### Calculate the Minimal Distance to the Closest End of the Segment

Since we do not know which point is on the left and which is on the right, we will make two variables to note this. Since the left point is always the one with the smaller x coordinate, we will use Math.Min(…) to find it. Accordingly, the right one is always the one with a larger x coordinate and we will use Math.Max(…). We will also find the distance from point x to the two points. Because we do not know their position relative to each other, we will use Math.Abs(…) to get a positive result.

The shorter of the two distances we can found using Math.Min(…).

### Determining if Point is In or Out the Segment and Print Result

It remains to be found whether the point is on or out of the line. The point will be on the line whenever it matches one of the other two points or its x coordinate lies between them. Otherwise, the point is outside the line. After checking, we get one of the two messages, which one is satisfied.

Finally, we print the distance previously found.

## Problem: Point in Figure

Write a program that checks whether a point (with coordinates x and y) is inside or outside the following figure:

### Input

The console reads two integers (one per line): x and y.

All input numbers are integers in the range [-1000 … 1000].

### Output

Print on the console "in" or "out" - whether the point is inside or outside the figure (the outline is inside).

### Sample Input and Output

Input Output Input Output
8
-5
in 6
-3
in
Input Output Input Output
11
-5
out 11
2
out

### Tips and Tricks

To find out if the point is in the figure, we will divide the figure into 2 quadrangles:

A sufficient condition is the point to be located in one of them to be in the figure.

### Determining Point Location

We read the input data from the console:

We will initialize two variables that will mark whether the point is in one of the rectangles.

When printing the message, we will check whether any of the variables has accepted a value of true. It is enough only one of them to be true to find the point in the figure.