Page 416 - CA_Blue( J )_Class10
P. 416
System.out.println("Sorted elements in descending order:");
for(i = 0; i < a.length; i++)
System.out.print(a[i] + "\t");
System.out.println();
}
}
24. Write a program to accept the names of 10 cities in a single dimension array and their STD (Subscriber Trunk Dialing) codes
in another single-dimension integer array. Search for a name of a city input by the user in the list. If found, display "Search
successful" and print the name of the city along with its STD code, or else display the message "Search unsuccessful, no such
city in the list". [2012]
Ans. import java.util.*;
class STD{
public static void main(){
Scanner sc = new Scanner(System.in);
String names[] = new String[10];
int codes[] = new int[10];
int i ,pos=-1;
String key;
for(i=0; i < 10; i++){
System.out.print("City: ");
names[i] = sc.next();
System.out.print("STD code: ");
codes[i] = sc.nextInt();
}
System.out.print("City to search: ");
key = sc.next();
for(i = 0; i < 10; i++){
if(key.equalsIgnoreCase(names[i]))
{ pos=i;
break;
}
}
if (pos != -1) {
System.out.println("Search successful");
System.out.println(names[i] + " - " + codes[i]);
}
else
System.out.println("Search unsuccessful, no such city in the list");
}
}
414414 Touchpad Computer Applications-X

