Java Program to Find Youngest and Eldest Members in Family
Java program to accept ages of family members and store them in one dimensional array and find the ages of youngest and eldest members in the family. The program below does exactly that, it sorts the array firs in ascending order using
Java program to find Youngers and eldest members in family using one dimensional array and for loop.
sort()
method from java.util.Arrays
class. Now we have age of youngest memeber as first element in array and eldest as last element in array. So we display the same.
Java Program to Find Youngest and Eldest Members in Family
import java.io.*;
import java.util.Arrays;
public class YoungerElderFamilyAge{
public static void main(String[] args) throws IOException {
int size;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many members are there in your family? ");
size = Integer.parseInt(br.readLine());
int[] age = new int[size];
//accept the ages of family memebers
for(int i=0;i<size;i++)
{
System.out.println("Enter age of " + (i+1) + "th member: " );
age[i] = Integer.parseInt(br.readLine());
}
//sorting the array in ascending order
Arrays.sort(age);
// Finding youngest and eldest members is equivalent to finding
// first and last elements of the array
System.out.println("Youngest member in the family is " + age[0]+ " years old");
System.out.println("Eldest member in the family is " + age[size-1]+ " years old");
}
}
/* Output of above code:-
How many members are there in your family? 5
Enter age of 1th member: 10
Enter age of 2th member: 20
Enter age of 3th member: 30
Enter age of 4th member: 32
Youngest member in the family is 10 years old
Eldest member in the family is 32 years old
*/
Java program to find Youngers and eldest members in family using one dimensional array and for loop.
import java.io.*;
import java.util.Arrays;
public class YoungerElderFamilyAge{
public static void main(String[] args) throws IOException {
int size;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many members are there in your family? ");
size = Integer.parseInt(br.readLine());
int[] age = new int[size];
//accept the ages of family memebers
for(int i=0;i<size;i++)
{
System.out.println("Enter age of " + (i+1) + "th member: " );
age[i] = Integer.parseInt(br.readLine());
}
// finding youngest and eldest memeber
// i.e. minimum and maximum elements in an array
// using for loop
int youngest = age[0];
int eldest = age[0];
for(int i=0;i<size;i++)
{
if(age[i]<youngest)
youngest = age[i];
if(age[i]>eldest)
eldest>= age[i];
}
System.out.println("Youngest member in the family is " + youngest + " years old");
System.out.println("Eldest member in the family is " + eldest + " years old");
}
}
/* Output of above code:-
How many members are there in your family? 5
Enter age of 1th member: 10
Enter age of 2th member: 20
Enter age of 3th member: 30
Enter age of 4th member: 32
Youngest member in the family is 10 years old
Eldest member in the family is 32 years old
*/