C PROGRAM • DECISION MAKING
Find the Largest of Three Numbers
Track a current largest value and update it after comparing each remaining number.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
14 39 27
Sample output
Enter three integers: 14 39 27 Largest = 39
C PROGRAM
Complete program
#include <stdio.h>
int main(void)
{
int first, second, third, largest;
printf("Enter three integers: " );
scanf("%d %d %d", &first, &second, &third);
largest = first;
if (second > largest)
largest = second;
if (third > largest)
largest = third;
printf("Largest = %d\n", largest);
return 0;
}CURRENT STEP
VARIABLES
| Scope | Name | Value |
|---|---|---|
| Not started | ||
CALL STACK
main()
PROGRAM OUTPUT
Waiting to run…
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read three integers.
- Assume first is the largest.
- Replace largest when second is greater.
- Replace largest when third is greater.
- Print largest.
The variable largest stores the best value seen so far. Each independent if statement compares one remaining input with that current value.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Using else-if when independent comparisons are intended.
Check this
Forgetting to initialize largest before comparing values.
