Implement a Java program to find the second largest number in an array.

Here’s a Java program to find the second largest number in an array: import java.util.Scanner; public class SecondLargest { public static void main(String[] args) { int[] arr = new int[5]; Scanner scanner = new Scanner(System.in); System.out.println(“Enter 5 numbers: “); for (int i = 0; i < 5; i++) { arr[i] = scanner.nextInt(); } int secondLargest ... Read more

Write a Java program to check if two strings are anagrams or not.

Here’s a Java program to check if two strings are anagrams or not: import java.util.Arrays; import java.util.Scanner; public class Anagram { public static void main(String[] args) { String str1, str2; Scanner scanner = new Scanner(System.in); System.out.print(“Enter the first string: “); str1 = scanner.nextLine(); System.out.print(“Enter the second string: “); str2 = scanner.nextLine(); if (isAnagram(str1, str2)) { … Read more

Implement a Java program to count the occurrence of each character in a string.

Here’s a Java program to count the occurrence of each character in a string: import java.util.HashMap; import java.util.Scanner; public class CharCount { public static void main(String[] args) { String str; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a string: “); str = scanner.nextLine(); HashMap charCount = new HashMap(); for (int i = 0; i < str.length(); ... Read more

Implement a Java program to sort an array of integers in ascending order.

Here’s a Java program to sort an array of integers in ascending order using the bubble sort algorithm: import java.util.Scanner; public class ArraySort { public static void main(String[] args) { int[] arr = new int[5]; Scanner scanner = new Scanner(System.in); System.out.println(“Enter 5 numbers: “); for (int i = 0; i < 5; i++) { arr[i] ... Read more

Write a Java program to find the largest and smallest number in an array.

Here’s a Java program to find the largest and smallest number in an array: import java.util.Scanner; public class ArrayMinMax { public static void main(String[] args) { int[] arr = new int[5]; int min, max; Scanner scanner = new Scanner(System.in); System.out.println(“Enter 5 numbers: “); for (int i = 0; i < 5; i++) { arr[i] = ... Read more

Implement a Java program to find the factorial of a number using recursion.

Here’s a Java program to find the factorial of a number using recursion: import java.util.Scanner; public class Factorial { public static void main(String[] args) { int num; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); num = scanner.nextInt(); long factorial = calculateFactorial(num); System.out.println(“Factorial of ” + num + ” = ” + factorial); } … Read more

Write a Java program to check if a given number is prime or not.

Here’s a Java program to check if a given number is prime or not: import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { int num; boolean isPrime = true; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); num = scanner.nextInt(); if (num

Write a Java program to find the Fibonacci series up to a given number.

Here’s a Java program to find the Fibonacci series up to a given number: import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { int n, t1 = 0, t2 = 1; Scanner scanner = new Scanner(System.in); System.out.print(“Enter the number of terms: “); n = scanner.nextInt(); System.out.print(“Fibonacci Series: “); for (int i = … Read more

Java 8 Features Optional class

The Optional class is a feature introduced in Java 8 that provides a way to handle null values more effectively. It is a container object that may or may not contain a non-null value. The purpose of Optional is to provide a simple way to write code that works correctly even if a variable may … Read more

Java 8 Features Default and static methods in interfaces

One of the major changes introduced in Java 8 was the ability to define default and static methods in interfaces. Prior to Java 8, interfaces could only contain abstract method declarations, which meant that any implementation had to be provided by a class that implemented the interface. With the introduction of default and static methods, … Read more