Challenging Java programs and succeeding in coding interviews-6

Java programs

Write a program that takes a string as input and counts the number of vowels and consonants

What are vowels?

vowels are the letters A, E, I, O, U, and sometimes Y.
They are called vowels because they represent sounds made with an open vocal tract, where the breath flows freely through the mouth.

What are consonants?

consonants are the letters B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, W, X, Y, and Z.
They represent sounds made with some kind of obstruction or closure in the vocal tract.

import java.util.Scanner;

public class javatechypid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String input = scanner.nextLine().toLowerCase();

        int vowelCount = 0;
        int consonantCount = 0;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);

            if (c >= 'a' && c <= 'z') {
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                    vowelCount++;
                } else {
                    consonantCount++;
                }
            }
        }

        System.out.println("Number of vowels: " + vowelCount);
        System.out.println("Number of consonants: " + consonantCount);
    }
}

Output:

Enter a string: techypid
Number of vowels: 2
Number of consonants: 6

Explanation of above java code:

This program prompts the user to enter a string and then counts the number of vowels and consonants in the string.

Prompt the user to enter a string and store the input in a String variable named “input”. Convert the input to lowercase using the toLowerCase() method.

Initialize two integer variables, “vowelCount” and “consonantCount”, to zero. These variables will be used to keep track of the number of vowels and consonants in the input string.

Use a for loop to iterate through each character in the input string.

For each character in the input string, check if it is a letter using the ASCII codes for letters. If the character is a letter, then check if it is a vowel or a consonant.

If the character is a vowel (i.e., ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), increment the “vowelCount” variable by 1.

If the character is a consonant, increment the “consonantCount” variable by 1.

What is a palindrome?

A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as forward. For example, “racecar” and “madam” are palindromic words, while “hello” is not a palindrome. Similarly, numbers such as 121, 1221, and 12321 are palindromic, while numbers like 123 and 4567 are not.

Java programs

Write a program that checks whether a given integer is a palindrome or not

import java.util.Scanner;

public class javatechypid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int num = scanner.nextInt();

        int temp = num;
        int reverse = 0;
        while (temp != 0) {
            int digit = temp % 10;
            reverse = reverse * 10 + digit;
            temp /= 10;
        }

        if (num == reverse) {
            System.out.println(num + " is a palindrome.");
        } else {
            System.out.println(num + " is not a palindrome.");
        }
    }
}

Output:

Enter an integer: 5
5 is a palindrome.

Enter an integer: 10
10 is not a palindrome.

Explanation of above java code:

Two variables are declared: “temp” to store a temporary copy of the input integer, and “reverse” to store the reverse of the input integer.

The program uses a while loop to reverse the input integer. The loop continues until the temporary integer “temp” becomes zero. Inside the loop, the last digit of “temp” is extracted using the modulo operator (%), and added to the “reverse” variable multiplied by 10. This way, the digit is added to the correct position of the reversed integer. After each iteration, “temp” is divided by 10 to remove the last digit.

Once the loop finishes, the “reverse” variable contains the reversed integer.

The program uses an if-else statement to check whether the original input integer “num” is equal to the reversed integer “reverse”. If they are equal, the program prints the message “num is a palindrome.” If they are not equal, the program prints the message “num is not a palindrome.”

Therefore, the output of the program will depend on the input integer. If the input integer is a palindrome, the program will print a message indicating that it is. If the input integer is not a palindrome, the program will print a message indicating that it is not. For example, if the user enters the number 5, the program will output “5 is a palindrome.” If the user enters the number 10, the program will output “10 is not a palindrome.”

Write a program to check if the given number is Armstrong number or not.

import java.util.Scanner;

public class javatechypid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        int sum = 0;
        int temp = num;
        int numDigits = (int) Math.floor(Math.log10(num)) + 1;

        while (temp != 0) {
            int digit = temp % 10;
            sum += Math.pow(digit, numDigits);
            temp /= 10;
        }

        if (num == sum) {
            System.out.println(num + " is an Armstrong number.");
        } else {
            System.out.println(num + " is not an Armstrong number.");
        }
    }
}

Output:

Enter a number: 5
5 is an Armstrong number.

Enter a number: 10
10 is not an Armstrong number.

Explanation of above java code:

This Java program checks whether a given number is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Three variables are declared: “sum” to store the sum of the digits raised to the power of the number of digits, “temp” to store a copy of the input number, and “numDigits” to store the number of digits in the input number. The Math.log10() method is used to determine the number of digits in the input number, and the Math.floor() method is used to round the result down to an integer.

The program uses a while loop to calculate the sum of the digits raised to the power of the number of digits. Inside the loop, the program uses the modulus operator to get the last digit of the number, raises it to the power of the number of digits, and adds it to the sum. The number is then divided by 10 to remove the last digit.

After the loop, the program checks whether the input number is equal to the sum of the digits raised to the power of the number of digits. If they are equal, the program prints “num is an Armstrong number.” If they are not equal, the program prints “num is not an Armstrong number.”

Therefore, the output of the program will depend on the input provided by the user. If the input number is an Armstrong number, the program will print a message indicating that the number is an Armstrong number. If the input number is not an Armstrong number, the program will print a message indicating that the number is not an Armstrong number.

Leave a Reply