In: Computer Science
Write a java code snippet to prompt 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.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//to
readinput
int n;
System.out.print("Enter number of
names you want to enter:");
n = sc.nextInt();
//creating array to store
names
String names[] = new
String[n];
//now reading names
for(int i=0;i<n;i++)
{
System.out.print("Enter
name:");
names[i] = sc.next();
}
//now displaying names in reverse
order
for(int i=n-1;0<=i;i--)
{
System.out.println(names[i]);
}
}
}
output:
Enter number of names you want to enter:5
Enter name:surya
Enter name:phani
Enter name:gopi
Enter name:boby
Enter name:chandu
chandu
boby
gopi
phani
surya