Conditions
Compare three integers while keeping track of the largest value seen so far.
Complete program
#include <stdio.h>
int main(void) {
int a, b, c;
printf("Three integers: ");
if (scanf("%d %d %d", &a, &b, &c) != 3) return 1;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest: %d\n", largest);
return 0;
}
Opens a new program without replacing your saved code. Press Run when you are ready.
How it works
Start with an actual input as the candidate, rather than zero. That matters when all three numbers are negative. Each comparison either preserves or replaces the current candidate.
Equal values need no special branch because the goal is the maximum value, not a unique winning position. Try 5 5 2 and then -8 -3 -10. This running-candidate pattern extends naturally to arrays.
Sample input
8 15 4Enter these values when prompted. The transcript below includes typed responses.
Expected output
Three integers: 8 15 4
Largest: 15Concepts to practice
- Comparison
- Assignment
- Maximum
Predict a different input or change one value, then compare the result with your prediction. Keep changes within the documented bounds and read warnings before trusting an answer.