Write a Java program to accep the name of 10 countries and their capital .Input name of countries and find whether ot exists in the list, if yes then display name of counrty and capital.
Answers
Answered by
3
Question:-
- Write a Java program to accept the name of 10 countries and their capital .Input name of countries and find whether ot exists in the list, if yes then display name of country and capital.
Program:-
import java.util.*;
class Country_Capital
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 countries and their capitals...");
String country[10];
String capital[10];
for(int i=0;i<10;i++)
{
System.out.print("Enter country: ");
country[i]=sc.nextLine();
System.out.print("Enter capital: ");
capital[i]=sc.nextLine();
}
System.out.print("Enter country and get the capital: ");
String s=sc.nextLine();
boolean found=false;
for(int i=0;i<10;i++)
{
if(country[i].equalsIgnoreCase(s))
{
System.out.println("Capital name is: "+capital[i]);
found=true;
break;
}
}
if(!found)
System.out.println("Not found in the list.");
}
}
Similar questions