PART A · PRESCRIBED EXERCISE 02
Find the largest of three numbers
Understand the algorithm. Run the code.
Explain the result with confidence.
LAB A02 · 01
Understand the task.
Aim: Find the largest of three numbers.
Maximum. The maximum of three numbers is a value greater than or equal to each of the three input values.
Review the related theory in Unit 2.
LAB A02 · 02
Know what goes in and what comes out.
Three integers, each from -10000 to 10000, separated by spaces or newlines.
Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.
LAB A02 · 03
Plan the steps before writing code.
- Read and validate a, b and c.
- If a is at least b and at least c, choose a.
- Otherwise, if b is at least a and at least c, choose b.
- Otherwise choose c, then display the chosen value.
LAB A02 · 04
One complete program. Ready to run.
#include <stdio.h>
int main(void)
{
int a, b, c, largest;
if (scanf("%d%d%d", &a, &b, &c) != 3 ||
a < -10000 || a > 10000 || b < -10000 || b > 10000 || c < -10000 || c > 10000) {
puts("Enter three integers from -10000 to 10000.");
return 1;
}
if (a >= b && a >= c) largest = a;
else if (b >= a && b >= c) largest = b;
else largest = c;
printf("Largest = %d\n", largest);
return 0;
}
Largest = 40
Compile on your own computer
gcc A02-largest-of-three.c -o programRun the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.
LAB A02 · 05
Connect each statement to its purpose.
- The && operator requires both comparisons to be true.
- Using >= handles ties, including three equal inputs.
- Negative values still compare normally; do not initialize a maximum to zero when all inputs may be negative.
- This task needs only the maximum value, not the position of every tied input.
LAB A02 · 06
Follow the sample calculation.
| Step or component | What happens |
|---|---|
| Inputs | a = 12, b = 40, c = 25 |
| First condition | 12 >= 40 is false; a is not selected. |
| Second condition | 40 >= 12 and 40 >= 25 are both true. |
| Result | largest = 40 |
LAB A02 · 07
Check more than one input.
| Try this input or case | Expected behaviour |
|---|---|
| -8 -2 -11 | Largest = -2 |
| 7 7 3 | Largest = 7 |
| 5 5 5 | Largest = 5 |
Time and storage
O(1) time and O(1) extra space.
Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.
LAB A02 · 08
Use the real C compiler.
Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.
Load the compiler when you’re ready to practise.
Your C workspace
Edit real C code and see real compiler output.
Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.
View / copy the selected source
#include <stdio.h>
int main(void)
{
int a, b, c, largest;
if (scanf("%d%d%d", &a, &b, &c) != 3 ||
a < -10000 || a > 10000 || b < -10000 || b > 10000 || c < -10000 || c > 10000) {
puts("Enter three integers from -10000 to 10000.");
return 1;
}
if (a >= b && a >= c) largest = a;
else if (b >= a && b >= c) largest = b;
else largest = c;
printf("Largest = %d\n", largest);
return 0;
}
LAB A02 · 09
Explain why the program works.
Why use && rather than ||?
To be the maximum, a must be at least both other inputs. Being at least only one is insufficient.
What happens when a and b tie for the largest?
The first matching condition selects their shared value. The required largest value remains correct.
Could a loop solve this?
Yes. Start from the first value and update the current maximum for each later value.
LAB A02 · 10
Build the next skill.
Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.