Write a program to create two single dimension array to store the name and age of 20 persons. Then display the name and age of only those persons whose age is more than 40.
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
String personNames[] = new String[20];
int personAges[] = new int[20];
for (int i = 0; i < 20; i++) {
System.out.print("Enter the name of person " + (i + 1) + ": ");
personNames[i] = keyBoard.nextLine();
System.out.print("Enter the age of person " + (i + 1) + ": ");
personAges[i] = keyBoard.nextInt();
keyBoard.nextLine();
}
System.out.println("The name and age of only those persons whose age is more than 40.");
for (int i = 0; i < 20; i++) {
if (personAges[i] > 40) {
System.out.println(personNames[i] + " is " + personAges[i] + " years old");
}
}
keyBoard.close();
}
}
Comments
Leave a comment