Header Ads

Write a program to store 5 names in an array and display it on screen


Approach :


In this problem we have to declare a Array with size 5.
Store the values to the array using for loop
Then print the Array using the another for loop


Code :


import java.util.Scanner;

public class nameArr {
public static void main(String[] args) {
String[] arr = new String[5];
Scanner sc = new Scanner(System.in);
System.out.println("Enter names to be stored :-");

for (int i = 0; i < 5; i++) {
arr[i] = sc.nextLine();
}

System.out.println();
System.out.println("Printing stored names ...");

for (int i = 0; i < 5; i++) {
System.out.println(arr[i]);
}
}
}


Output :

Enter names to be stored :-
Abhi
Anku
Abhay
Nabu
Aditya

Printing stored names ...
Abhi
Anku
Abhay
Nabu
Aditya

Post a Comment

0 Comments