Computer Applications
Write a program in Java to store 10 different country names and their capitals in two different Single Dimensional Arrays (SDA). Display the country names (that starts with a vowel) along with their capitals in the given format.
Country Names Capital
xxxx xxxx
xxxx xxxx
Answer
import java.util.Scanner;
public class KboatCountryCapital
{
public static void main(String args[]) {
final int SIZE = 10;
Scanner in = new Scanner(System.in);
String countries[] = new String[SIZE];
String capitals[] = new String[SIZE];
System.out.println("Enter " + SIZE
+ " countries and their capitals");
for (int i = 0; i < SIZE; i++) {
System.out.print("Enter country name: ");
countries[i] = in.nextLine();
System.out.print("Enter its capital: ");
capitals[i] = in.nextLine();
}
System.out.println("Country Names\t\tCapital");
for (int i = 0; i < SIZE; i++) {
char ch = Character.toUpperCase(countries[i].charAt(0));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
System.out.println(countries[i] + "\t\t" + capitals[i]);
}
}
}
}
Variable Description Table
Program Explanation
Output
Related Questions
Write a program in Java to store 20 different names and telephone numbers of your friends in two different Single Dimensional Arrays (SDA). Now arrange all the names in alphabetical order and display all the names along with their respective telephone numbers using selection sort technique.
Write a program to accept 10 names in a Single Dimensional Array (SDA). Display the names whose first letter matches with the letter entered by the user.
Sample Input:
Aman Shahi
Akash Gupta
Suman Mishra
and so on ……….Sample Output:
Enter the alphabet: A
Aman Shahi
Akash Gupta
…..
…..Using the switch statement, write a menu driven program for the following:
(a) To print the Floyd’s triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15(b) To display the following pattern:
I
I C
I C S
I C S EFor an incorrect option, an appropriate error message should be displayed.
Write a program to generate a triangle or an inverted triangle based upon User’s choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter a word : BLUEJ
Sample Output:
B
L L
U U U
E E E E
J J J J JExample 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter a word : BLUEJ
Sample Output:
B L U E J
B L U E
B L U
B L
B