- Find the maximum number in an array:Â
Java Code
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
- Reverse a string:Â
Java Code
public static String reverseString(String str) {
return new StringBuilder(str).reverse().toString();
}
- Check if a number is prime:Â
Java Code
public static boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
- Check if a string is palindrome:Â
Java Code
public static boolean isPalindrome(String str) {
return str.equals(new StringBuilder(str).reverse().toString());
}
- Find factorial of a number:Â
Java Code
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n – 1);
}
- Calculate Fibonacci series:Â
Java Code
public static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n – 1) + fibonacci(n – 2);
}
- Check if two strings are anagrams:Â
Java Code
public static boolean areAnagrams(String str1, String str2) {
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
- Count the occurrence of a character in a string:Â
Java Code
public static int countCharOccurrence(String str, char c) {
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == c) count++;
}
return count;
}
- Remove duplicates from an array:Â
Java Code
public static int[] removeDuplicates(int[] arr) {
Set<Integer> set = new LinkedHashSet<>();
for (int num : arr) {
set.add(num);
}
return set.stream().mapToInt(Integer::intValue).toArray();
}
- Find intersection of two arrays:Â
Java Code
public static int[] findIntersection(int[] arr1, int[] arr2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int num : arr1) {
set1.add(num);
}
for (int num : arr2) {
if (set1.contains(num)) {
set2.add(num);
}
}
return set2.stream().mapToInt(Integer::intValue).toArray();
}
- Reverse words in a string:Â
Java Code
public static String reverseWords(String str) {
String[] words = str.split(“\\s+”);
StringBuilder reversed = new StringBuilder();
for (int i = words.length – 1; i >= 0; i–) {
reversed.append(words[i]).append(” “);
}
return reversed.toString().trim();
}
- Check if a number is Armstrong:Â
Java Code
public static boolean isArmstrong(int num) {
int original = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, 3); // For 3-digit Armstrong number
num /= 10;
}
return sum == original;
}
- Find the second largest number in an array:Â
Java Code
public static int findSecondLargest(int[] arr) {
Arrays.sort(arr);
return arr[arr.length – 2];
}
- Check if a number is even or odd:Â
Java Code
public static String checkEvenOdd(int num) {
return (num % 2 == 0) ? “Even” : “Odd”;
}
- Check if a year is a leap year:Â
Java Code
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
- Find GCD (Greatest Common Divisor) of two numbers:Â
Java Code
public static int findGCD(int a, int b) {
if (b == 0) return a;
return findGCD(b, a % b);
}
- Convert decimal to binary:Â
Java Code
public static String decimalToBinary(int num) {
return Integer.toBinaryString(num);
}
- Convert binary to decimal:Â
Java Code
public static int binaryToDecimal(String binary) {
return Integer.parseInt(binary, 2);
}
- Check if a string contains only digits:Â
Java Code
public static boolean containsOnlyDigits(String str) {
return str.matches(“[0-9]+”);
}
- Check if a string contains only alphabets:Â
Java Code
public static boolean containsOnlyAlphabets(String str) {
return str.matches(“[a-zA-Z]+”);
}