Challenging Java programs and succeeding in coding interviews-2

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

import java.util.Scanner;

public class javatechypid {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      
      System.out.print("Enter a string: ");
      String str = input.nextLine();
      
      str = str.toLowerCase();
      
      int vowelCount = 0, consonantCount = 0;
      for (int i = 0; i < str.length(); i++) {
         char ch = str.charAt(i);
         if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowelCount++;
         } else if (ch >= 'a' && ch <= 'z') {
            consonantCount++;
         }
      }
      
      System.out.println("The string contains " + vowelCount + " vowels and " + consonantCount + " consonants.");
   }
}

Output:

Enter a string: Hello World
The string contains 3 vowels and 7 consonants.

Explanation of above java code:

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

Here’s a step-by-step breakdown of the code:

The program starts by importing the Scanner class from the java.util package, which allows the program to read user input from the console.

The main method is declared with the signature “public static void main(String[] args)”. This is the entry point for the Java program and any code inside this method will be executed when the program is run.

A new Scanner object named “input” is created to read user input from the console.

The program prompts the user to enter a string using the System.out.print() method and reads the input using the Scanner object’s nextLine() method. The input string is stored in a variable called “str”.

The program converts the input string to lowercase using the toLowerCase() method. This ensures that all characters in the string are in the same case for easier comparison.

Two variables are declared to keep track of the number of vowels and consonants in the string. Both are initialized to 0.

A for loop is used to iterate over each character in the string. The loop runs from i = 0 to i < str.length(), which means it will iterate once for each character in the string.

Inside the loop, the program uses the charAt() method to get the character at index i in the string.

An if-else statement is used to determine whether the character is a vowel or a consonant. If the character is a vowel (‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), the vowelCount variable is incremented. If the character is a letter (between ‘a’ and ‘z’), but not a vowel, the consonantCount variable is incremented.

After the loop has finished iterating over all the characters in the string, the program prints the results using the System.out.println() method. The output string contains the number of vowels and consonants found in the input string.

Write a program that generates a random number between 1 and 100, and asks the user to guess the number.

output of program: import java.util.Scanner;
import java.util.Random;

public class javatechypid {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      Random random = new Random();
      
      int num = random.nextInt(100) + 1;
      int guess;
      
      do {
         System.out.print("Guess the number (between 1 and 100): ");
         guess = input.nextInt();
         if (guess < num) {
            System.out.println("Too low! Try again.");
         } else if (guess > num) {
            System.out.println("Too high! Try again.");
         }
      } while (guess != num);
      
      System.out.println("Congratulations! You guessed the number.");
   }
}

Output:

Guess the number (between 1 and 100): 50
Too high! Try again.
Guess the number (between 1 and 100): 25
Too high! Try again.
Guess the number (between 1 and 100): 10
Too low! Try again.
Guess the number (between 1 and 100): 15
Too low! Try again.
Guess the number (between 1 and 100): 20
Congratulations! You guessed the number.

Explanation of above java code:

The program starts by importing the Scanner class and the Random class from the java.util package.

The main method is declared with the signature “public static void main(String[] args)”. This is the entry point for the Java program and any code inside this method will be executed when the program is run.

Two objects are created: a Scanner object named “input” to read user input from the console, and a Random object named “random” to generate a random number between 1 and 100.

The program generates a random number using the Random object’s nextInt() method and stores it in a variable called “num”. The “+1” ensures that the generated number will be between 1 and 100, inclusive.

Two variables are declared: “guess” to store the user’s input, and “count” to keep track of the number of guesses the user has made.

The program starts a do-while loop that will continue to prompt the user for input until they correctly guess the number generated by the program.

Inside the loop, the program prompts the user to guess the number using the System.out.print() method and reads the input using the Scanner object’s nextInt() method. The input is stored in the “guess” variable.

The program uses an if-else statement to check whether the user’s guess is too high, too low, or equal to the generated number. If the guess is too low, the program prints “Too low! Try again.” If the guess is too high, the program prints “Too high! Try again.” If the guess is correct, the loop will terminate.

After each guess, the program will continue to prompt the user for input until they correctly guess the number.

Once the user correctly guesses the number, the program will print “Congratulations! You guessed the number.

Leave a Reply