KnowledgeBoat Logo

Computer Applications

Write a program in Java to input the names of 10 cities in a Single Dimensional Array. Display only those names which begin with a consonant but end with a vowel.
Sample Input: Kolkata, Delhi, Bengaluru, Jamshedpur, Bokaro, …….
Sample Output: Kolkata
Delhi
Bengaluru
Bokaro
….
….

Java

Java String Handling

32 Likes

Answer

import java.util.Scanner;

public class KboatCityName
{
    public static boolean isVowel(char ch) {
        
        char letter = Character.toUpperCase(ch);
        
        if (letter == 'A' ||
            letter == 'E' ||
            letter == 'I' ||
            letter == 'O' ||
            letter == 'U')
            return true;
            
        return false;       
    }
    
    public static void main(String args[]) {
        String cities[] = new String[10];
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 10 city names");
        
        for (int i = 0; i < cities.length; i++) {
            cities[i] = in.nextLine();
        }
        
        System.out.println("\nCities starting with consonant & ending with vowel:");
        for (int i = 0; i < cities.length; i++) {
            if (!isVowel(cities[i].charAt(0)) && 
                isVowel(cities[i].charAt(cities[i].length() - 1)))
            System.out.println(cities[i]);
        }
    }

}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to input the names of 10 cities in a Single Dimensional Array. Display only those names which begin with a consonant but end with a vowel. Sample Input: Kolkata, Delhi, Bengaluru, Jamshedpur, Bokaro, ……. Sample Output: Kolkata Delhi Bengaluru Bokaro …. ….

Answered By

9 Likes


Related Questions