easy
2 views
Find the oldest age in the family
Find the maximum age among N family members given as space-separated integers
Understand the Problem
Problem Statement
A family consists of N members. Their ages are passed as input separated by a space. Write a program to find the age of the oldest member in the family.
Constraints
- All ages must be between 0 and 110 (inclusive)
- Input consists of N space-separated integers
- N can be any positive integer
Examples
Example 1
Input
10 78 56 87 32Output
87Explanation
Among the ages 10, 78, 56, 87, and 32, the oldest age is 87.
Example 2
Input
25 45 33 67 18Output
67Explanation
Among the ages 25, 45, 33, 67, and 18, the oldest age is 67.
Example 3
Input
100 95 88Output
100Explanation
Among the ages 100, 95, and 88, the oldest age is 100.
Solution
#include <stdio.h>
#include <limits.h>
int main() {
int age;
int max_age = INT_MIN;
// Read ages until end of input
while (scanf("%d", &age) == 1) {
if (age > max_age) {
max_age = age;
}
}
printf("%d", max_age);
return 0;
}Time:O(n)
Space:O(1)
Approach:
The C solution reads integers from standard input using scanf() in a loop. It initializes max_age to the smallest possible integer value. For each age read, it compares with the current maximum and updates max_age if the new age is larger. Finally, it prints the maximum age found.
Visual Explanation
Loading diagram...