THE SYLLABUS QUESTION
What you need to solve
Write a program for finding the max and min from the three numbers.
Three integers between -1,000,000,000 and 1,000,000,000, separated by spaces.
UNDERSTAND THE IDEA
Explanation
Begin with the first value as both the minimum and maximum. Compare the other two values independently with each current result.
Independent comparisons handle ties and negative values naturally. No sorting is necessary.
PLAN BEFORE CODING
Algorithm
- Read and validate three integers.
- Set minimum and maximum to the first number.
- Compare the second and third numbers with both results.
- Print the maximum and minimum.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
int main(void) {
long long a, b, c, minimum, maximum;
if (scanf("%lld %lld %lld", &a, &b, &c) != 3 ||
a < -1000000000LL || a > 1000000000LL ||
b < -1000000000LL || b > 1000000000LL ||
c < -1000000000LL || c > 1000000000LL) {
puts("Invalid input.");
return 1;
}
minimum = maximum = a;
if (b < minimum) minimum = b;
if (c < minimum) minimum = c;
if (b > maximum) maximum = b;
if (c > maximum) maximum = c;
printf("Maximum: %lld\nMinimum: %lld\n", maximum, minimum);
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 maximum-minimum-three.c -o lab
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Start: 12 | minimum = 12 | maximum = 12 |
| Read -3 | minimum = -3 | maximum = 12 |
| Read 27 | minimum = -3 | maximum = 27 |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
12 -3 27
Maximum: 27
Minimum: -3
Sample 2
5 5 5
Maximum: 5
Minimum: 5
Sample 3
-8 -2 -15
Maximum: -2
Minimum: -15
Common mistakes
- Do not initialize the minimum to zero: all-positive inputs would give the wrong answer.
- Do not use one else-if chain that skips independent comparisons.
WHY THIS GROWTH RATE?
Time and space complexity
Time O(1); auxiliary space O(1).
The input always contains exactly three numbers. The program makes four comparisons after initialization: b and c are each compared with the minimum and the maximum. A larger numeric value does not create extra iterations.
A fixed number of reads, comparisons and assignments gives O(1) time. The five scalar variables occupy constant working space, so auxiliary space is O(1).
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.