WAP to input ten (10) city names in array and print them
Answers
Answered by
1
Solution:
The given problem is solved in Java.
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int i, n = 10;
String arr[] = new String[n];
System.out.println("Enter 10 cities name..");
for(i = 0;i < n;i++){
System.out.print("> ");
arr[i] = sc.nextLine();
}
System.out.print("The cities are: ");
for(String j : arr)
System.out.print(j + " ");
}
}
Explanation:
- Line 1: Imports scanner class for taking input.
- Line 2: Class declaration.
- Line 3: Main method declaration.
- Line 4: Object of Scanner class created.
- Line 5-6: Variables for the program are created.
- Line 7: Requesting the user to enter 10 cities name.
- Line 8-11: The city names are entered and stored in array.
- Line 12-14: The city names are displayed using for-each loop.
- Line 15: End of main method.
- Line 16: End of class.
Refer to the attachment for output.
Attachments:

Similar questions