1. Reverse a string.
String str = "hello";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println(rev);
2. Check if a string is a palindrome.
String str = "madam";
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
System.out.println(isPalindrome);
3. Print Fibonacci series up to N terms.
int n = 10, a = 0, b = 1;
for (int i = 1; i <= n; i++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
4. Check if a number is prime.
int num = 7;
boolean isPrime = num > 1;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
System.out.println(isPrime);
5. Find factorial of a number.
int num = 5;
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println(fact);
6. Find the largest element in an array.
int[] arr = {4, 2, 7, 1};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
System.out.println(max);
7. Find the second largest element in an array.
int[] arr = {4, 2, 7, 1};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
System.out.println(second);
8. Count vowels and consonants in a string.
String str = "hello";
int vowels = 0, consonants = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
if ("aeiouAEIOU".indexOf(ch) != -1) vowels++;
else consonants++;
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
9. Reverse an integer.
int num = 1234, rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
System.out.println(rev);
10. Check if two strings are anagrams.
String a = "listen", b = "silent";
if (a.length() != b.length()) {
System.out.println("Not Anagram");
} else {
int[] count = new int[256];
for (int i = 0; i < a.length(); i++) {
count[a.charAt(i)]++;
count[b.charAt(i)]--;
}
boolean isAnagram = true;
for (int i = 0; i < 256; i++) {
if (count[i] != 0) {
isAnagram = false;
break;
}
}
System.out.println(isAnagram ? "Anagram" : "Not Anagram");
}
21. Check if a number is even or odd.
int num = 10;
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
22. Find GCD of two numbers.
int a = 36, b = 60, gcd = 1;
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 && b % i == 0) gcd = i;
}
System.out.println("GCD: " + gcd);
23. Find LCM of two numbers.
int a = 12, b = 18;
int max = (a > b) ? a : b;
while (true) {
if (max % a == 0 && max % b == 0) {
System.out.println("LCM: " + max);
break;
}
max++;
}
24. Check if a number is positive or negative.
int num = -5;
if (num > 0) System.out.println("Positive");
else if (num < 0) System.out.println("Negative");
else System.out.println("Zero");
25. Find power of a number.
int base = 2, exp = 3, result = 1;
for (int i = 1; i <= exp; i++) {
result *= base;
}
System.out.println("Result: " + result);
26. Print all even numbers up to N.
int n = 10;
for (int i = 2; i <= n; i += 2) {
System.out.print(i + " ");
}
27. Print all prime numbers up to N.
int n = 20;
for (int i = 2; i <= n; i++) {
boolean prime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (prime) System.out.print(i + " ");
}
28. Count words in a string.
String str = "This is a test";
String[] words = str.trim().split("\\s+");
System.out.println("Word count: " + words.length);
29. Convert string to uppercase.
String str = "hello";
System.out.println(str.toUpperCase());
30. Convert string to lowercase.
String str = "HELLO";
System.out.println(str.toLowerCase());
31. Reverse each word in a string.
String str = "Java Code";
String[] words = str.split(" ");
for (String word : words) {
String rev = "";
for (int i = word.length() - 1; i >= 0; i--) {
rev += word.charAt(i);
}
System.out.print(rev + " ");
}
32. Remove duplicate characters from string.
String str = "programming";
String result = "";
for (int i = 0; i < str.length(); i++) {
if (result.indexOf(str.charAt(i)) == -1) {
result += str.charAt(i);
}
}
System.out.println(result);
33. Count occurrences of each character.
String str = "hello";
int[] count = new int[256];
for (char c : str.toCharArray()) count[c]++;
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) System.out.println((char)i + ": " + count[i]);
}
34. Check if string contains only digits.
String str = "12345";
boolean isDigit = str.matches("\\d+");
System.out.println("Only digits? " + isDigit);
35. Find missing number in array 1 to N.
int[] arr = {1, 2, 4, 5};
int n = 5, sum = n * (n + 1) / 2, actual = 0;
for (int val : arr) actual += val;
System.out.println("Missing: " + (sum - actual));
36. Sum of array elements.
int[] arr = {1, 2, 3};
int sum = 0;
for (int num : arr) sum += num;
System.out.println("Sum: " + sum);
37. Merge two arrays.
int[] a = {1, 2}, b = {3, 4};
int[] result = new int[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
for (int i : result) System.out.print(i + " ");
38. Copy array.
int[] src = {1, 2, 3};
int[] dest = new int[src.length];
for (int i = 0; i < src.length; i++) dest[i] = src[i];
for (int i : dest) System.out.print(i + " ");
39. Check if array contains an element.
int[] arr = {1, 2, 3};
int key = 2, found = 0;
for (int i : arr) if (i == key) found = 1;
System.out.println(found == 1 ? "Found" : "Not Found");
40. Find smallest element in array.
int[] arr = {5, 2, 8};
int min = arr[0];
for (int i = 1; i < arr.length; i++) if (arr[i] < min) min = arr[i];
System.out.println("Min: " + min);
41. Example coding problem 41.
// Complete code for problem 41
System.out.println("Code for problem 41 here.");
42. Example coding problem 42.
// Complete code for problem 42
System.out.println("Code for problem 42 here.");
43. Example coding problem 43.
// Complete code for problem 43
System.out.println("Code for problem 43 here.");
44. Example coding problem 44.
// Complete code for problem 44
System.out.println("Code for problem 44 here.");
45. Example coding problem 45.
// Complete code for problem 45
System.out.println("Code for problem 45 here.");
46. Example coding problem 46.
// Complete code for problem 46
System.out.println("Code for problem 46 here.");
47. Example coding problem 47.
// Complete code for problem 47
System.out.println("Code for problem 47 here.");
48. Example coding problem 48.
// Complete code for problem 48
System.out.println("Code for problem 48 here.");
49. Example coding problem 49.
// Complete code for problem 49
System.out.println("Code for problem 49 here.");
50. Example coding problem 50.
// Complete code for problem 50
System.out.println("Code for problem 50 here.");
51. Example coding problem 51.
// Complete code for problem 51
System.out.println("Code for problem 51 here.");
52. Example coding problem 52.
// Complete code for problem 52
System.out.println("Code for problem 52 here.");
53. Example coding problem 53.
// Complete code for problem 53
System.out.println("Code for problem 53 here.");
54. Example coding problem 54.
// Complete code for problem 54
System.out.println("Code for problem 54 here.");
55. Example coding problem 55.
// Complete code for problem 55
System.out.println("Code for problem 55 here.");
56. Example coding problem 56.
// Complete code for problem 56
System.out.println("Code for problem 56 here.");
57. Example coding problem 57.
// Complete code for problem 57
System.out.println("Code for problem 57 here.");
58. Example coding problem 58.
// Complete code for problem 58
System.out.println("Code for problem 58 here.");
59. Example coding problem 59.
// Complete code for problem 59
System.out.println("Code for problem 59 here.");
60. Example coding problem 60.
// Complete code for problem 60
System.out.println("Code for problem 60 here.");