Got it 👍 You listed a set of Java practice problems. I’ll give you clear explanations + example Java programs for each one.
1) Find the Smallest Number in an Array
public class SmallestInArray {
public static void main(String[] args) {
int[] arr = {12, 5, 8, 1, 45, -3, 9};
int min = arr[0];
for (int num : arr) {
if (num < min) {
min = num;
}
}
System.out.println("Smallest number: " + min);
}
}
2) Fibonacci Sequence
(a) Recursive
public class FibonacciRecursive {
static int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int n = 10;
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + " ");
}
}
}
(b) Iterative
public class FibonacciIterative {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}
3) Remove Even Numbers from an Array
import java.util.*;
public class RemoveEven {
public static void main(String[] args) {
int[] arr = {12, 7, 5, 6, 9, 10};
ArrayList<Integer> result = new ArrayList<>();
for (int num : arr) {
if (num % 2 != 0) {
result.add(num);
}
}
System.out.println("Array without evens: " + result);
}
}
4) Find Duplicate Number in an Array
import java.util.*;
public class FindDuplicate {
public static void main(String[] args) {
int[] arr = {4, 3, 6, 2, 1, 6, 7, 3};
HashSet<Integer> seen = new HashSet<>();
System.out.print("Duplicates: ");
for (int num : arr) {
if (!seen.add(num)) {
System.out.print(num + " ");
}
}
}
}
5) Move Negative Numbers to Front of Array
import java.util.*;
public class MoveNegatives {
public static void main(String[] args) {
int[] arr = {3, -2, -5, 7, -1, 4};
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}
System.out.println("After moving negatives: " + Arrays.toString(arr));
}
}
6) Longest Palindromic Substring
public class LongestPalindrome {
public static String longestPalindrome(String s) {
if (s == null || s.length() < 1) return "";
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expand(s, i, i);
int len2 = expand(s, i, i+1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len-1)/2;
end = i + len/2;
}
}
return s.substring(start, end+1);
}
private static int expand(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}
public static void main(String[] args) {
String s = "babad";
System.out.println("Longest Palindrome: " + longestPalindrome(s));
}
}
7) Remove Vowels from a String
public class RemoveVowels {
public static void main(String[] args) {
String str = "Hello World";
String result = str.replaceAll("[AEIOUaeiou]", "");
System.out.println("Without vowels: " + result);
}
}
8) Validate Phone Number Format (Example: XXX-XXX-XXXX
)
import java.util.regex.*;
public class PhoneValidator {
public static void main(String[] args) {
String phone = "123-456-7890";
String regex = "\\d{3}-\\d{3}-\\d{4}";
if (phone.matches(regex)) {
System.out.println("Valid Phone Number");
} else {
System.out.println("Invalid Phone Number");
}
}
}
✅ Now you have Java solutions for all problems.
Do you want me to combine all into a single Menu-driven Program
where you choose which problem to run?
Top comments (0)