In: Computer Science
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Java code:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input=new
Scanner(System.in);
//asking for count of
names
System.out.println("Enter the number of names you would like to
enter: ");
//accepting it
int
num=input.nextInt();
//creating an array to
store names
String[] names=new
String[num];
//skipping current
line
input.nextLine();
//loop to accept each
name
for(int
i=0;i<num;i++){
//asking for name
System.out.print("Enter the name: ");
//accepting it
names[i]=input.nextLine();
}
//printing Names in
reverse order
System.out.println("Names in reverse order is: ");
//loop to print names in
reverse order
for(int
i=num-1;i>=0;i--){
//printing the names
System.out.println(names[i]);
}
}
}
Screenshot:
Input and Output: