Implement a Java program to remove duplicates from an array.

Here’s a Java program to remove duplicates from an array: import java.util.Arrays; import java.util.Scanner; public class RemoveDuplicates { 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[] newArr = … Read more

Write a Java program to reverse a number.

Here’s a Java program to reverse a number: import java.util.Scanner; public class ReverseNumber { public static void main(String[] args) { int num, reversedNum = 0; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); num = scanner.nextInt(); while (num != 0) { int digit = num % 10; reversedNum = reversedNum * 10 + digit; … Read more

Write a Java program to find the sum of digits of a given number.

Here’s a Java program to find the sum of digits of a given number: import java.util.Scanner; public class SumOfDigits { public static void main(String[] args) { int num, sum = 0; Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); num = scanner.nextInt(); while (num > 0) { int digit = num % 10; sum … Read more

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 <= 1) { isPrime = false; } else { … Read more