0% found this document useful (0 votes)
23 views66 pages

Solutions of Hundreds

The document contains a series of Java code examples demonstrating various programming concepts and algorithms. These include basic operations like printing 'Hello, World!', summing two numbers, checking for even or odd, calculating factorials, and more advanced topics such as sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort) and searching algorithms (Binary Search). Each example is structured with a main method and utilizes the Scanner class for user input.

Uploaded by

Neeraj singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views66 pages

Solutions of Hundreds

The document contains a series of Java code examples demonstrating various programming concepts and algorithms. These include basic operations like printing 'Hello, World!', summing two numbers, checking for even or odd, calculating factorials, and more advanced topics such as sorting algorithms (Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort) and searching algorithms (Binary Search). Each example is structured with a main method and utilizes the Scanner class for user input.

Uploaded by

Neeraj singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

1.

Hello World
java
RunCopy code
1public class HelloWorld {
2 public static void main(String[] args) {
3 System.out.println("Hello, World!");
4 }
5}
2. Sum of Two Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfTwoNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter first number: ");
7 int a = scanner.nextInt();
8 System.out.print("Enter second number: ");
9 int b = scanner.nextInt();
10 System.out.println("Sum: " + (a + b));
11 }
12}
3. Even or Odd
java
RunCopy code
1import java.util.Scanner;
2
3public class EvenOrOdd {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 if (num % 2 == 0) {
9 System.out.println(num + " is even.");
10 } else {
11 System.out.println(num + " is odd.");
12 }
13 }
14}
4. Factorial
java
RunCopy code
1import java.util.Scanner;
2
3public class Factorial {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int factorial = 1;
9 for (int i = 1; i <= num; i++) {
10 factorial *= i;
11 }
12 System.out.println("Factorial: " + factorial);
13 }
14}
5. Prime Number Check
java
RunCopy code
1import java.util.Scanner;
2
3public class PrimeNumberCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 boolean isPrime = true;
9 if (num <= 1) {
10 isPrime = false;
11 } else {
12 for (int i = 2; i <= Math.sqrt(num); i++) {
13 if (num % i == 0) {
14 isPrime = false;
15 break;
16 }
17 }
18 }
19 System.out.println(num + (isPrime ? " is prime." : " is not prime."));
20 }
21}
6. Fibonacci Series
java
RunCopy code
1import java.util.Scanner;
2
3public class FibonacciSeries {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of terms: ");
7 int n = scanner.nextInt();
8 int a = 0, b = 1;
9 System.out.print("Fibonacci Series: " + a + ", " + b);
10 for (int i = 2; i < n; i++) {
11 int c = a + b;
12 System.out.print(", " + c);
13 a = b;
14 b = c;
15 }
16 }
17}
7. Reverse a String
java
RunCopy code
1import java.util.Scanner;
2
3public class ReverseString {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String reversed = new StringBuilder(str).reverse().toString();
9 System.out.println("Reversed String: " + reversed);
10 }
11}
8. Palindrome Check
java
RunCopy code
1import java.util.Scanner;
2
3public class PalindromeCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String reversed = new StringBuilder(str).reverse().toString();
9 System.out.println(str + (str.equals(reversed) ? " is a palindrome." : " is not a
palindrome."));
10 }
11}
9. Count Vowels and Consonants
java
RunCopy code
1import java.util.Scanner;
2
3public class CountVowelsConsonants {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 int vowels = 0, consonants = 0;
9 for (char c : str.toCharArray()) {
10 if (Character.isLetter(c)) {
11 if ("AEIOU aeiou".indexOf(c) != -1) {
12 vowels++;
13 } else {
14 consonants++;
15 }
16 }
17 }
18 System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
19 }
20}
10. Find Largest Element in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class LargestElementInArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int largest = arr[0];
14 for (int i = 1; i < n; i++) {
15 if (arr[i] > largest) {
16 largest = arr[i];
17 }
18 }
19 System.out.println("Largest element: " + largest);
20 }
21}
11. Bubble Sort
java
RunCopy code
1import java.util.Arrays;
2
3public class BubbleSort {
4 public static void main(String[] args) {
5 int[] arr = {64, 34, 25, 12, 22, 11, 90};
6 int n = arr.length;
7 for (int i = 0; i < n - 1; i++) {
8 for (int j = 0; j < n - i - 1; j++) {
9 if (arr[j] > arr[j + 1]) {
10 // swap arr[j+1] and arr[j]
11 int temp = arr[j];
12 arr[j] = arr[j + 1];
13 arr[j + 1] = temp;
14 }
15 }
16 }
17 System.out.println("Sorted array: " + Arrays.toString(arr));
18 }
19}
12. Selection Sort
java
RunCopy code
1import java.util.Arrays;
2
3public class SelectionSort {
4 public static void main(String[] args) {
5 int[] arr = {64, 25, 12, 22, 11};
6 int n = arr.length;
7 for (int i = 0; i < n - 1; i++) {
8 int minIndex = i;
9 for (int j = i + 1; j < n; j++) {
10 if (arr[j] < arr[minIndex]) {
11 minIndex = j;
12 }
13 }
14 // swap arr[minIndex] and arr[i]
15 int temp = arr[minIndex];
16 arr[minIndex] = arr[i];
17 arr[i] = temp;
18 }
19 System.out.println("Sorted array: " + Arrays.toString(arr));
20 }
21}
13. Insertion Sort
java
RunCopy code
1import java.util.Arrays;
2
3public class InsertionSort {
4 public static void main(String[] args) {
5 int[] arr = {12, 11, 13, 5, 6};
6 int n = arr.length;
7 for (int i = 1; i < n; ++i) {
8 int key = arr[i];
9 int j = i - 1;
10 while (j >= 0 && arr[j] > key) {
11 arr[j + 1] = arr[j];
12 j = j - 1;
13 }
14 arr[j + 1] = key;
15 }
16 System.out.println("Sorted array: " + Arrays.toString(arr));
17 }
18}
14. Merge Sort
java
RunCopy code
1import java.util.Arrays;
2
3public class MergeSort {
4 public static void main(String[] args) {
5 int[] arr = {38, 27, 43, 3, 9, 82, 10};
6 mergeSort(arr, 0, arr.length - 1);
7 System.out.println("Sorted array: " + Arrays.toString(arr));
8 }
9
10 public static void mergeSort(int[] arr, int left, int right) {
11 if (left < right) {
12 int mid = (left + right) / 2;
13 mergeSort(arr, left, mid);
14 mergeSort(arr, mid + 1, right);
15 merge(arr, left, mid, right);
16 }
17 }
18
19 public static void merge(int[] arr, int left, int mid, int right) {
20 int n1 = mid - left + 1;
21 int n2 = right - mid;
22 int[] L = new int[n1];
23 int[] R = new int[n2];
24 for (int i = 0; i < n1; ++i) {
25 L[i] = arr[left + i];
26 }
27 for (int j = 0; j < n2; ++j) {
28 R[j] = arr[mid + 1 + j];
29 }
30 int i = 0, j = 0;
31 int k = left;
32 while (i < n1 && j < n2) {
33 if (L[i] <= R[j]) {
34 arr[k] = L[i];
35 i++;
36 } else {
37 arr[k] = R[j];
38 j++;
39 }
40 k++;
41 }
42 while (i < n1) {
43 arr[k] = L[i];
44 i++;
45 k++;
46 }
47 while (j < n2) {
48 arr[k] = R[j];
49 j++;
50 k++;
51 }
52 }
53}
15. Quick Sort
java
RunCopy code
1import java.util.Arrays;
2
3public class QuickSort {
4 public static void main(String[] args) {
5 int[] arr = {10, 7, 8, 9, 1, 5};
6 int n = arr.length;
7 quickSort(arr, 0, n - 1);
8 System.out.println("Sorted array: " + Arrays.toString(arr));
9 }
10
11 public static void quickSort(int[] arr, int low, int high) {
12 if (low < high) {
13 int pi = partition(arr, low, high);
14 quickSort(arr, low, pi - 1);
15 quickSort(arr, pi + 1, high);
16 }
17 }
18
19 public static int partition(int[] arr, int low, int high) {
20 int pivot = arr[high];
21 int i = (low - 1);
22 for (int j = low; j < high; j++) {
23 if (arr[j] < pivot) {
24 i++;
25 int temp = arr[i];
26 arr[i] = arr[j];
27 arr[j] = temp;
28 }
29 }
30 int temp = arr[i + 1];
31 arr[i + 1] = arr[high];
32 arr[high] = temp;
33 return i + 1;
34 }
35}
16. Binary Search
java
RunCopy code
1import java.util.Arrays;
2
3public class BinarySearch {
4 public static void main(String[] args) {
5 int[] arr = {2, 3, 4, 10, 40};
6 int x = 10;
7 int result = binarySearch(arr, 0, arr.length - 1, x);
8 if (result == -1) {
9 System.out.println("Element not present in array");
10 } else {
11 System.out.println("Element found at index " + result);
12 }
13 }
14
15 public static int binarySearch(int[] arr, int left, int right, int x) {
16 if (right >= left) {
17 int mid = left + (right - left) / 2;
18 if (arr[mid] == x) {
19 return mid;
20 }
21 if (arr[mid] > x) {
22 return binarySearch(arr, left, mid - 1, x);
23 }
24 return binarySearch(arr, mid + 1, right, x);
25 }
26 return -1;
27 }
28}
17. Count Occurrences of a Character in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountCharacterOccurrences {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 System.out.print("Enter a character to count: ");
9 char ch = scanner.next().charAt(0);
10 int count = 0;
11 for (char c : str.toCharArray()) {
12 if (c == ch) {
13 count++;
14 }
15 }
16 System.out.println("Occurrences of '" + ch + "': " + count);
17 }
18}
18. Find GCD of Two Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class GCD {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter first number: ");
7 int a = scanner.nextInt();
8 System.out.print("Enter second number: ");
9 int b = scanner.nextInt();
10 System.out.println("GCD: " + gcd(a, b));
11 }
12
13 public static int gcd(int a, int b) {
14 if (b == 0) {
15 return a;
16 }
17 return gcd(b, a % b);
18 }
19}
19. Check Armstrong Number
java
RunCopy code
1import java.util.Scanner;
2
3public class ArmstrongNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number : ");
7 int num = scanner.nextInt();
8 int originalNum = num;
9 int sum = 0;
10 int n = String.valueOf(num).length();
11 while (num != 0) {
12 int digit = num % 10;
13 sum += Math.pow(digit, n);
14 num /= 10;
15 }
16 System.out.println(originalNum + (sum == originalNum ? " is an Armstrong number." :
" is not an Armstrong number."));
17 }
18}
20. Find LCM of Two Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class LCM {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter first number: ");
7 int a = scanner.nextInt();
8 System.out.print("Enter second number: ");
9 int b = scanner.nextInt();
10 int lcm = (a * b) / gcd(a, b);
11 System.out.println("LCM: " + lcm);
12 }
13
14 public static int gcd(int a, int b) {
15 if (b == 0) {
16 return a;
17 }
18 return gcd(b, a % b);
19 }
20}
21. Convert Decimal to Binary
java
RunCopy code
1import java.util.Scanner;
2
3public class DecimalToBinary {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a decimal number: ");
7 int num = scanner.nextInt();
8 String binary = Integer.toBinaryString(num);
9 System.out.println("Binary representation: " + binary);
10 }
11}
22. Convert Binary to Decimal

java
RunCopy code
1import java.util.Scanner;
2
3public class BinaryToDecimal {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a binary number: ");
7 String binary = scanner.nextLine();
8 int decimal = Integer.parseInt(binary, 2);
9 System.out.println("Decimal representation: " + decimal);
10 }
11}
23. Count Digits in a Number
java
RunCopy code
1import java.util.Scanner;
2
3public class CountDigits {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int count = String.valueOf(num).length();
9 System.out.println("Number of digits: " + count);
10 }
11}
24. Sum of Digits in a Number
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfDigits {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int sum = 0;
9 while (num != 0) {
10 sum += num % 10;
11 num /= 10;
12 }
13 System.out.println("Sum of digits: " + sum);
14 }
15}
25. Check Perfect Number
java
RunCopy code
1import java.util.Scanner;
2
3public class PerfectNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int sum = 0;
9 for (int i = 1; i <= num / 2; i++) {
10 if (num % i == 0) {
11 sum += i;
12 }
13 }
14 System.out.println(num + (sum == num ? " is a perfect number." : " is not a perfect
number."));
15 }
16}
26. Find the Second Largest Number in an Array

java
RunCopy code
1import java.util.Scanner;
2
3public class SecondLargestInArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int largest = Integer.MIN_VALUE;
14 int secondLargest = Integer.MIN_VALUE;
15 for (int num : arr) {
16 if (num > largest) {
17 secondLargest = largest;
18 largest = num;
19 } else if (num > secondLargest && num < largest) {
20 secondLargest = num;
21 }
22 }
23 System.out.println("Second largest element: " + secondLargest);
24 }
25}
27. Remove Duplicates from an Array
java
RunCopy code
1import java.util.Arrays;
2import java import java.util.HashSet;
3import java.util.Scanner;
4
5public class RemoveDuplicates {
6 public static void main(String[] args) {
7 Scanner scanner = new Scanner(System.in);
8 System.out.print("Enter the number of elements: ");
9 int n = scanner.nextInt();
10 int[] arr = new int[n];
11 System.out.println("Enter the elements:");
12 for (int i = 0; i < n; i++) {
13 arr[i] = scanner.nextInt();
14 }
15 HashSet<Integer> set = new HashSet<>();
16 for (int num : arr) {
17 set.add(num);
18 }
19 System.out.println("Array after removing duplicates: " + set);
20 }
21}
28. Find the Missing Number in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class MissingNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements (0 to " + n + "):");
10 for (int i = 0; i < n - 1; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int total = n * (n + 1) / 2;
14 int sum = 0;
15 for (int num : arr) {
16 sum += num;
17 }
18 System.out.println("Missing number: " + (total - sum));
19 }
20}
29. Find the Intersection of Two Arrays
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class IntersectionOfArrays {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter the number of elements in first array: ");
8 int n1 = scanner.nextInt();
9 int[] arr1 = new int[n1];
10 System.out.println("Enter the elements of first array:");
11 for (int i = 0; i < n1; i++) {
12 arr1[i] = scanner.nextInt();
13 }
14 System.out.print("Enter the number of elements in second array: ");
15 int n2 = scanner.nextInt();
16 int[] arr2 = new int[n2];
17 System.out.println("Enter the elements of second array:");
18 for (int i = 0; i < n2; i++) {
19 arr2[i] = scanner.nextInt();
20 }
21 HashSet<Integer> set = new HashSet<>();
22 for (int num : arr1) {
23 set.add(num);
24 }
25 System.out.print("Intersection: ");
26 for (int num : arr2) {
27 if (set.contains(num)) {
28 System.out.print(num + " ");
29 }
30 }
31 }
32}
30. Rotate an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class RotateArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 System.out.print("Enter the number of positions to rotate: ");
14 int d = scanner.nextInt();
15 d = d % n; // In case d >= n
16 reverse(arr, 0, n - 1);
17 reverse(arr, 0, n - d - 1);
18 reverse(arr, n - d, n - 1);
19 System.out.println("Rotated array: ");
20 for (int num : arr) {
21 System.out.print(num + " ");
22 }
23 }
24
25 public static void reverse(int[] arr, int start, int end) {
26 while (start < end) {
27 int temp = arr[start];
28 arr[start] = arr[end];
29 arr[end] = temp;
30 start++;
31 end--;
32 }
33 }
34}
31. Find the Kth Largest Element in an Array
java
RunCopy code
1import java.util.Arrays;
2import java.util.Scanner;
3
4public class KthLargestElement {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter the number of elements: ");
8 int n = scanner.nextInt();
9 int[] arr = new int[n];
10 System.out.println("Enter the elements:");
11 for (int i = 0; i < n; i++) {
12 arr[i] = scanner.nextInt();
13 }
14 System.out.print("Enter the value of K: ");
15 int k = scanner.nextInt();
16 Arrays.sort(arr);
17 System.out.println("The " + k + "th largest element is: " + arr[n - k]);
18 }
19}
32. Check if a String is an Anagram
java
RunCopy code
1import java.util.Arrays;
2import java.util.Scanner;
3
4public class AnagramCheck {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter first string: ");
8 String str1 = scanner.nextLine();
9 System.out.print("Enter second string: ");
10 String str2 = scanner.nextLine();
11 char[] charArray1 = str1.toCharArray();
12 char[] charArray2 = str2.toCharArray();
13 Arrays.sort(charArray1);
14 Arrays.sort(charArray2);
15 boolean isAnagram = Arrays.equals(charArray1, charArray2);
16 System.out.println("The strings are " + (isAnagram ? "anagrams." : "not anagrams."));
17 }
18}
33. Count the Number of Words in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountWords {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String[] words = str.trim().split("\\s+");
9 System.out.println("Number of words: " + words.length);
10 }
11}
34. Find the Longest Word in a String

java
RunCopy code
1import java.util.Scanner;
2
3public class LongestWord {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String[] words = str.split("\\s+");
9 String longestWord = "";
10 for (String word : words) {
11 if (word.length() > longestWord.length()) {
12 longestWord = word;
13 }
14 }
15 System.out.println("Longest word: " + longestWord);
16 }
17}
35. Check if a String is a Substring
java
RunCopy code
1import java.util.Scanner;
2
3public class SubstringCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter main string: ");
7 String mainStr = scanner.nextLine();
8 System.out.print("Enter substring: ");
9 String subStr = scanner.nextLine();
10 boolean isSubstring = mainStr.contains(subStr);
11 System.out.println("The substring is " + (isSubstring ? "present." : "not present."));
12 }
13}
36. Find the Frequency of Each Character in a String
java
RunCopy code
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class CharacterFrequency {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a string: ");
8 String str = scanner.nextLine();
9 HashMap<Character, Integer> frequencyMap = new HashMap<>();
10 for (char c : str.toCharArray()) {
11 frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
12 }
13 System.out.println("Character frequencies: " + frequencyMap);
14 }
15}
37. Find the First Non-Repeated Character in a String
java
RunCopy code
1import java.util.LinkedHashMap;
2import java.util.Map;
3import java.util.Scanner;
4
5public class FirstNonRepeatedCharacter {
6 public static void main(String[] args) {
7 Scanner scanner = new Scanner(System.in);
8 System.out.print("Enter a string: ");
9 String str = scanner.nextLine();
10 LinkedHashMap<Character, Integer> charCountMap = new LinkedHashMap<>();
11 for (char c : str.toCharArray()) {
12 charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
13 }
14 char firstNonRepeated = '\0';
15 for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
16 if (entry.getValue() == 1) {
17 firstNonRepeated = entry.getKey();
18 break;
19 }
20 }
21 System.out.println("First non-repeated character: " + (firstNonRepeated != '\0' ?
firstNonRepeated : "None"));
22 }
23}
38. Check if Two Strings are Rotations of Each Other
java
RunCopy code
1import java.util.Scanner;
2
3public class RotationCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter first string: ");
7 String str1 = scanner.nextLine();
8 System.out.print("Enter second string: ");
9String str2 = scanner.nextLine();
10 if (str1.length() != str2.length()) {
11 System.out.println("The strings are not rotations of each other.");
12 } else {
13 String concatenated = str1 + str1;
14 boolean isRotation = concatenated.contains(str2);
15 System.out.println("The strings are " + (isRotation ? "rotations." : "not rotations."));
16 }
17 }
18}
39. Find the Intersection of Two Strings
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class IntersectionOfStrings {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter first string: ");
8 String str1 = scanner.nextLine();
9 System.out.print("Enter second string: ");
10 String str2 = scanner.nextLine();
11 HashSet<Character> set = new HashSet<>();
12 for (char c : str1.toCharArray()) {
13 set.add(c);
14 }
15 System.out.print("Intersection: ");
16 for (char c : str2.toCharArray()) {
17 if (set.contains(c)) {
18 System.out.print(c + " ");
19 set.remove(c); // To avoid duplicates in output
20 }
21 }
22 }
23}
40. Count the Number of Occurrences of Each Word in a String
java
RunCopy code
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class WordFrequency {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a string: ");
8 String str = scanner.nextLine();
9 String[] words = str.trim().split("\\s+");
10 HashMap<String, Integer> wordCountMap = new HashMap<>();
11 for (String word : words) {
12 wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);
13 }
14 System.out.println("Word frequencies: " + wordCountMap);
15 }
16}
41. Check if a String is a Valid Number
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidNumberCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 boolean isValidNumber = str.matches("-?\\d+(\\.\\d+)?");
9 System.out.println("The string is " + (isValidNumber ? "a valid number." : "not a valid
number."));
10 }
11}
42. Find the Sum of Even and Odd Numbers in an Array

java
RunCopy code
1import java.util.Scanner;
2
3public class SumEvenOdd {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int sumEven = 0, sumOdd = 0;
14 for (int num : arr) {
15 if (num % 2 == 0) {
16 sumEven += num;
17 } else {
18 sumOdd += num;
19 }
20 }
21 System.out.println("Sum of even numbers: " + sumEven);
22 System.out.println("Sum of odd numbers: " + sumOdd);
23 }
24}
43. Find the Maximum and Minimum in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class MaxMinInArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int max = arr[0];
14 int min = arr[0];
15 for (int num : arr) {
16 if (num > max) {
17 max = num;
18 }
19 if (num < min) {
20 min = num;
21 }
22 }
23 System.out.println("Maximum: " + max);
24 System.out.println("Minimum: " + min);
25 }
26}
44. Check if a Number is a Power of Two

java
RunCopy code
1import java.util.Scanner;
2
3public class PowerOfTwo {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 boolean isPowerOfTwo = (num > 0) && ((num & (num - 1)) == 0);
9 System.out.println(num + (isPowerOfTwo ? " is a power of two." : " is not a power of
two."));
10 }
11}
45. Find the Sum of the First N Natural Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfNaturalNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int n = scanner.nextInt();
8 int sum = n * (n + 1) / 2;
9 System.out.println("Sum of first " + n + " natural numbers: " + sum);
10 }
11}
46. Check if a Number is a Palindrome

java
RunCopy code
1import java.util.Scanner;
2
3public class PalindromeNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int originalNum = num;
9 int reversedNum = 0;
10 while (num != 0) {
11 int digit = num % 10;
12 reversedNum = reversedNum * 10 + digit;
13 num /= 10;
14 }
15 System.out.println(originalNum + (originalNum == reversedNum ? " is a palindrome."
: " is not a palindrome."));
16 }
17}
47. Find the Sum of the Digits of a Number
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfDigitsInNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int sum = 0;
9 while (num != 0) {
10 sum += num % 10;
11 num /= 10;
12 }
13 System.out.println("Sum of digits: " + sum);
14 }
15}
48. Find the Factorial of a Number Using Recursion
java
RunCopy code
1import java.util.Scanner;
2
3public class FactorialRecursion {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 System.out.println("Factorial: " + factorial(num));
9 }
10
11 public static int factorial(int n) {
12 if (n == 0) {
13 return 1;
14 }
15 return n * factorial(n - 1);
16 }
17}
49. Check if a Number is a Strong Number
java
RunCopy code
1import java.util.Scanner;
2
3public class StrongNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int originalNum = num;
9 int sum = 0;
10 while (num != 0) {
11 int digit = num % 10;
12 sum += factorial(digit);
13 num /= 10;
14 }
15 System.out.println(originalNum + (sum == originalNum ? " is a strong number." : " is
not a strong number."));
16 }
17
18 public static int factorial(int n) {
19 if (n == 0) {
20 return 1;
21 }
22 return n * factorial(n - 1);
23 }
24}
50. Find the Nth Fibonacci Number Using Recursion
java
RunCopy code
1import java.util.Scanner;
2
3public class FibonacciRecursion {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the term number: ");
7 int n = scanner.nextInt();
8 System.out.println("Fibonacci number: " + fibonacci(n));
9 }
10
11 public static int fibonacci(int n) {
12 if (n <= 1) {
13 return n;
14 }
15 return fibonacci(n - 1) + fibonacci(n - 2);
16 }
17}
51. Check if a Number is a Perfect Square
java
RunCopy code
1import java.util.Scanner;
2
3public class PerfectSquare {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 double sqrt = Math.sqrt(num);
9 boolean isPerfectSquare = (sqrt * sqrt == num);
10 System.out.println(num + (isPerfectSquare ? " is a perfect square." : " is not a perfect
square."));
11 }
12}
52. Find the Sum of Prime Numbers up to N java import java.util.Scanner;
public class SumOfPrimes { public static void main(String[] args) { Scanner scanner = new
Scanner(System.in); System.out.print("Enter a number: "); int n = scanner.nextInt(); int sum
= 0; for (int i = 2; i <= n; i++) { if (isPrime(i)) { sum += i; } } System.out.println("Sum of prime
numbers up to " + n + ": " + sum); }
RunCopy code
1 public static boolean isPrime(int num) {
2 if (num <= 1) return false;
3 for (int i = 2; i <= Math.sqrt(num); i++) {
4 if (num % i == 0) return false;
5 }
6 return true;
7 }
}
RunCopy code
1
253. **Find the GCD of Two Numbers Using Recursion**
3```java
4import java.util.Scanner;
5
6public class GCDRecursion {
7 public static void main(String[] args) {
8 Scanner scanner = new Scanner(System.in);
9 System.out.print("Enter first number: ");
10 int a = scanner.nextInt();
11 System.out.print("Enter second number: ");
12 int b = scanner.nextInt();
13 System.out.println("GCD: " + gcd(a, b));
14 }
15
16 public static int gcd(int a, int b) {
17 if (b == 0) return a;
18 return gcd(b, a % b);
19 }
20}
54. Find the LCM of Two Numbers Using GCD
java
RunCopy code
1import java.util.Scanner;
2
3public class LCMUsingGCD {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter first number: ");
7 int a = scanner.nextInt();
8 System.out.print("Enter second number: ");
9 int b = scanner.nextInt();
10 int lcm = (a * b) / gcd(a, b);
11 System.out.println("LCM: " + lcm);
12 }
13
14 public static int gcd(int a, int b) {
15 if (b == 0) return a;
16 return gcd(b, a % b);
17 }
18}
55. Check if a Number is a Happy Number
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class HappyNumber {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a number: ");
8 int num = scanner.nextInt();
9 System.out.println(num + (isHappy(num) ? " is a happy number." : " is not a happy
number."));
10 }
11
12 public static boolean isHappy(int n) {
13 HashSet<Integer> seen = new HashSet<>();
14 while (n != 1 && !seen.contains(n)) {
15 seen.add(n);
16 n = getSumOfSquares(n);
17 }
18 return n == 1;
19 }
20
21 public static int getSumOfSquares(int n) {
22 int sum = 0;
23 while (n > 0) {
24 int digit = n % 10;
25 sum += digit * digit;
26 n /= 10;
27 }
28 return sum;
29 }
30}
56. Find the Sum of the Digits of a Number Using Recursion
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfDigitsRecursion {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 System.out.println("Sum of digits: " + sumOfDigits(num));
9 }
10
11 public static int sumOfDigits(int n) {
12 if (n == 0) return 0;
13 return n % 10 + sumOfDigits(n / 10);
14 }
15}
57. Find the Nth Fibonacci Number Using Dynamic Programming
java
RunCopy code
1import java.util.Scanner;
2
3public class FibonacciDynamic {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the term number: ");
7 int n = scanner.nextInt();
8 System.out.println("Fibonacci number: " + fibonacci(n));
9 }
10
11 public static int fibonacci(int n) {
12 if (n <= 1) return n;
13 int[] fib = new int[n + 1];
14 fib[0] = 0;
15 fib[1] = 1;
16 for (int i = 2; i <= n; i++) {
17 fib[i] = fib[i - 1] + fib[i - 2];
18 }
19 return fib[n];
20 }
21}
58. java
RunCopy code
1undefined
import java.util.Scanner;
public class FibonacciDynamic { public static void main(String[] args) { Scanner scanner =
new Scanner(System.in); System.out.print("Enter the term number: "); int n =
scanner.nextInt(); System.out.println("Fibonacci number: " + fibonacci(n)); }
RunCopy code
1 public static int fibonacci(int n) {
2 if (n <= 1) return n;
3 int[] fib = new int[n + 1];
4 fib[0] = 0;
5 fib[1] = 1;
6 for (int i = 2; i <= n; i++) {
7 fib[i] = fib[i - 1] + fib[i - 2];
8 }
9 return fib[n];
10 }
}
RunCopy code
1
259. **Find the Largest Palindrome in a String**
3```java
4import java.util.Scanner;
5
6public class LargestPalindrome {
7 public static void main(String[] args) {
8 Scanner scanner = new Scanner(System.in);
9 System.out.print("Enter a string: ");
10 String str = scanner.nextLine();
11 String largestPalindrome = "";
12 for (int i = 0; i < str.length(); i++) {
13 for (int j = i; j < str.length(); j++) {
14 String substring = str.substring(i, j + 1);
15 if (isPalindrome(substring) && substring.length() > largestPalindrome.length()) {
16 largestPalindrome = substring;
17 }
18 }
19 }
20 System.out.println("Largest palindrome: " + largestPalindrome);
21 }
22
23 public static boolean isPalindrome(String s) {
24 String reversed = new StringBuilder(s).reverse().toString();
25 return s.equals(reversed);
26 }
27}
60. Find the Most Frequent Character in a String
java
RunCopy code
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class MostFrequentCharacter {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a string: ");
8 String str = scanner.nextLine();
9 HashMap<Character, Integer> frequencyMap = new HashMap<>();
10 for (char c : str.toCharArray()) {
11 frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
12 }
13 char mostFrequent = '\0';
14 int maxCount = 0;
15 for (char c : frequencyMap.keySet()) {
16 if (frequencyMap.get(c) > maxCount) {
17 maxCount = frequencyMap.get(c);
18 mostFrequent = c;
19 }
20 }
21 System.out.println("Most frequent character: " + mostFrequent);
22 }
23}
61. Check if a String is a Valid Email
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidEmailCheck {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter an email address: ");
7 String email = scanner.nextLine();
8 boolean isValid = email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
9 System.out.println("The email address is " + (isValid ? "valid." : "not valid."));
10 }
11}
62. Count the Number of Digits in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountDigitsInString {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 int count = 0;
9 for (char c : str.toCharArray()) {
10 if (Character.isDigit(c)) {
11 count++;
12 }
13 }
14 System.out.println("Number of digits: " + count);
15 }
16}
63. Find the Length of the Longest Substring Without Repeating Characters
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class LongestSubstringWithoutRepeating {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a string: ");
8 String str = scanner.nextLine();
9 System.out.println("Length of longest substring without repeating characters: " +
lengthOfLongestSubstring(str));
10 }
11
12 public static int lengthOfLongestSubstring(String s) {
13 HashSet<Character> set = new HashSet<>();
14 int left = 0, maxLength = 0;
15 for (int right = 0; right < s.length(); right++) {
16 while (set.contains(s.charAt(right))) {
17 set.remove(s.charAt(left));
18 left++;
19 }
20 set.add(s.charAt(right));
21 maxLength = Math.max(maxLength, right - left + 1);
22 }
23 return maxLength;
24 }
25}
64. Find the First Repeated Character in a String
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class FirstRepeatedCharacter {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a string: ");
8 String str = scanner.nextLine();
9 char firstRepeated = '\0';
10 HashSet<Character> set = new ```java
11 HashSet<Character> set = new HashSet<>();
12 for (char c : str.toCharArray()) {
13 if (set.contains(c)) {
14 firstRepeated = c;
15 break;
16 }
17 set.add(c);
18 }
19 System.out.println("First repeated character: " + (firstRepeated != '\0' ? firstRepeated
: "None"));
20 }
21}
65. Check if a String is a Valid Palindrome
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidPalindrome {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String cleaned = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
9 boolean isPalindrome = cleaned.equals(new
StringBuilder(cleaned).reverse().toString());
10 System.out.println("The string is " + (isPalindrome ? "a palindrome." : "not a
palindrome."));
11 }
12}
66. Find the Number of Vowels in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountVowels {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 int count = 0;
9 for (char c : str.toLowerCase().toCharArray()) {
10 if ("aeiou".indexOf(c) != -1) {
11 count++;
12 }
13 }
14 System.out.println("Number of vowels: " + count);
15 }
16}
67. Find the Number of Consonants in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountConsonants {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 int count = 0;
9 for (char c : str.toLowerCase().toCharArray()) {
10 if (Character.isLetter(c) && "aeiou".indexOf(c) == -1) {
11 count++;
12 }
13 }
14 System.out.println("Number of consonants: " + count);
15 }
16}
68. Find the Sum of Odd Numbers in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfOddNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int sum = 0;
14 for (int num : arr) {
15 if (num % 2 != 0) {
16 sum += num;
17 }
18 }
19 System.out.println("Sum of odd numbers: " + sum);
20 }
21}
69. Find the Sum of Even Numbers in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfEvenNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int sum = 0;
14 for (int num : arr) {
15 if (num % 2 == 0) {
16 sum += num;
17 }
18 }
19 System.out.println("Sum of even numbers: " + sum);
20 }
21}
70. Find the Average of Numbers in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class AverageOfArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 double sum = 0;
14 for (int num : arr) {
15 sum += num;
16 }
17 double average = sum / n;
18 System.out.println("Average: " + average);
19 }
20}
71. Find the Median of Numbers in an Array
java
RunCopy code
1import java.util.Arrays ```java
2import java.util.Scanner;
3
4public class MedianOfArray {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter the number of elements: ");
8 int n = scanner.nextInt();
9 int[] arr = new int[n];
10 System.out.println("Enter the elements:");
11 for (int i = 0; i < n; i++) {
12 arr[i] = scanner.nextInt();
13 }
14 Arrays.sort(arr);
15 double median;
16 if (n % 2 == 0) {
17 median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0;
18 } else {
19 median = arr[n / 2];
20 }
21 System.out.println("Median: " + median);
22 }
23}
72. Find the Mode of Numbers in an Array
java
RunCopy code
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class ModeOfArray {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter the number of elements: ");
8 int n = scanner.nextInt();
9 int[] arr = new int[n];
10 System.out.println("Enter the elements:");
11 for (int i = 0; i < n; i++) {
12 arr[i] = scanner.nextInt();
13 }
14 HashMap<Integer, Integer> frequencyMap = new HashMap<>();
15 for (int num : arr) {
16 frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
17 }
18 int mode = arr[0];
19 int maxCount = 0;
20 for (int key : frequencyMap.keySet()) {
21 if (frequencyMap.get(key) > maxCount) {
22 maxCount = frequencyMap.get(key);
23 mode = key;
24 }
25 }
26 System.out.println("Mode: " + mode);
27 }
28}
73. Find the Range of Numbers in an Array
java
RunCopy code
1import java.util.Scanner;
2
3public class RangeOfArray {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter the number of elements: ");
7 int n = scanner.nextInt();
8 int[] arr = new int[n];
9 System.out.println("Enter the elements:");
10 for (int i = 0; i < n; i++) {
11 arr[i] = scanner.nextInt();
12 }
13 int max = arr[0];
14 int min = arr[0];
15 for (int num : arr) {
16 if (num > max) {
17 max = num;
18 }
19 if (num < min) {
20 min = num;
21 }
22 }
23 int range = max - min;
24 System.out.println("Range: " + range);
25 }
26}
74. Find the Sum of the First N Odd Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfFirstNOddNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int n = scanner.nextInt();
8 int sum = 0;
9 for (int i = 1; i <= n; i++) {
10 sum += (2 * i - 1);
11 }
12 System.out.println("Sum of first " + n + " odd numbers: " + sum);
13 }
14}
75. Find the Sum of the First N Even Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfFirstNEvenNumbers {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int n = scanner.nextInt();
8 int sum = 0;
9 for (int i = 1; i <= n; i++) {
10 sum += (2 * i);
11 }
12 System.out.println("Sum of first " + n + " even numbers: " + sum);
13 }
14}
76. Find the Product of the Digits of a Number
java
RunCopy code
1import java.util.Scanner;
2
3public class ProductOfDigits {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int product = 1;
9 while (num != 0) {
10 product *= num % 10;
11 num /= 10;
12 }
13 System.out.println("Product of digits: " + product);
14 }
15}
77. Check if a Number is a Neon Number

java
RunCopy code
1import java.util.Scanner;
2
3```java
4public class NeonNumber {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a number: ");
8 int num = scanner.nextInt();
9 int square = num * num;
10 int sum = 0;
11 while (square != 0) {
12 sum += square % 10;
13 square /= 10;
14 }
15 System.out.println(num + (sum == num ? " is a neon number." : " is not a neon
number."));
16 }
17}
78. Check if a Number is a Harshad Number
java
RunCopy code
1import java.util.Scanner;
2
3public class HarshadNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int sum = 0;
9 int temp = num;
10 while (temp != 0) {
11 sum += temp % 10;
12 temp /= 10;
13 }
14 System.out.println(num + (num % sum == 0 ? " is a Harshad number." : " is not a
Harshad number."));
15 }
16}
79. Find the Sum of the First N Prime Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfFirstNPrimes {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int n = scanner.nextInt();
8 int count = 0, num = 2, sum = 0;
9 while (count < n) {
10 if (isPrime(num)) {
11 sum += num;
12 count++;
13 }
14 num++;
15 }
16 System.out.println("Sum of first " + n + " prime numbers: " + sum);
17 }
18
19 public static boolean isPrime(int num) {
20 if (num <= 1) return false;
21 for (int i = 2; i <= Math.sqrt(num); i++) {
22 if (num % i == 0) return false;
23 }
24 return true;
25 }
26}
80. Find the Sum of the Digits of a Number Using Streams

java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfDigitsUsingStreams {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 int sum = String.valueOf(num).chars().map(Character::getNumericValue).sum();
9 System.out.println("Sum of digits: " + sum);
10 }
11}
81. Check if a Number is a Palindrome Using Streams
java
RunCopy code
1import java.util.Scanner;
2
3public class PalindromeUsingStreams {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int num = scanner.nextInt();
8 String str = String.valueOf(num);
9 boolean isPalindrome = str.equals(new StringBuilder(str).reverse().toString());
10 System.out.println(num + (isPalindrome ? " is a palindrome." : " is not a
palindrome."));
11 }
12}
82. Find the First Non-Repeated Character Using Streams
java
RunCopy code
1import java.util.LinkedHashMap;
2import java.util.Map;
3import java.util.Scanner;
4
5public class FirstNonRepeatedCharacterUsingStreams {
6 public static void main(String[] args) {
7 Scanner scanner = new Scanner(System.in);
8 System.out.print("Enter a string: ");
9 String str = scanner.nextLine();
10 Map<Character, Long> frequencyMap = str.chars()
11 .mapToObj(c -> (char) c)
12 .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting()));
13 Character firstNonRepeated = frequencyMap.entrySet().stream()
14 .filter(entry -> entry.getValue() == 1)
15 .map(Map.Entry::getKey)
16 .findFirst()
17 .orElse(null);
18 System.out.println("First non-repeated character: " + (firstNonRepeated != null ?
firstNonRepeated : "None"));
19 }
20}
83. Find the Last Occurrence of a Character in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class LastOccurrence {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 System.out.print("Enter a character to find: ");
9 char ch = scanner.next().charAt(0);
10 int lastIndex = str.lastIndexOf(ch);
11 System.out.println("Last occurrence of '" + ch + "': " + (lastIndex != -1 ? lastIndex :
"not found"));
12 }
13}
84. Find the First Occurrence of a Character in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class FirstOccurrence {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 System.out.print("Enter a character to find: ");
9 char ch = scanner.next().charAt(0);
10 int firstIndex = str.indexOf(ch);
11 System.out.println("First occurrence of '" + ch + "': " + (firstIndex != -1 ? firstIndex :
"not found"));
12 }
13}
85. Count the Number of Uppercase and Lowercase Letters in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class CountUpperLower {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 int upperCount = 0, lowerCount = 0;
9 for (char c : str.toCharArray()) {
10 if (Character.isUpperCase(c)) {
11 upperCount++;
12 } else if (Character.isLowerCase(c)) {
13 lowerCount++;
14 }
15 }
16 System.out.println("Uppercase letters: " + upperCount);
17 System.out.println("Lowercase letters: " + lowerCount);
18 }
19}
86. Reverse the Words in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class ReverseWords {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String[] words = str.split(" ");
9 StringBuilder reversed = new StringBuilder();
10 for (int i = words.length - 1; i >= 0; i--) {
11 reversed.append(words[i]).append(" ");
12 }
13 System.out.println("Reversed words: " + reversed.toString().trim());
14 }
15}
87. Check if Two Strings are Anagrams Using Sorting
java
RunCopy code
1import java.util.Arrays;
2import java.util.Scanner;
3
4public class AnagramCheckSorting {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter first string: ");
8 String str1 = scanner.nextLine();
9 System.out.print("Enter second string: ");
10 String str2 = scanner.nextLine();
11 char[] charArray1 = str1.toCharArray();
12 char[] charArray2 = str2.toCharArray();
13 Arrays.sort(charArray1);
14 Arrays.sort(charArray2);
15 boolean isAnagram = Arrays.equals(charArray1, charArray2);
16 System.out.println("The strings are " + (isAnagram ? "anagrams." : "not anagrams."));
17 }
18}
88. Find the Length of Each Word in a String
java
RunCopy code
1import java.util.Scanner;
2
3public class LengthOfEachWord {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 String[] words = str.split(" ");
9 for (String word : words) {
10 System.out.println("Length of '" + word + "': " + word.length());
11 }
12 }
13}
89. Check if a String Contains Only Digits
java
RunCopy code
1import java.util.Scanner;
2
3public class ContainsOnlyDigits {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a string: ");
7 String str = scanner.nextLine();
8 boolean isOnlyDigits = str.chars().allMatch(Character::isDigit);
9 System.out.println("The string contains " + (isOnlyDigits ? "only digits." : "non-digit
characters."));
10 }
11}
90. Find the Number of Words in a Sentence
java
RunCopy code
1import java.util.Scanner;
2
3public class CountWordsInSentence {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a sentence: ");
7 String sentence = scanner.nextLine();
8 String[] words = sentence.trim().split("\\s+");
9 System.out.println("Number of words: " + words.length);
10 }
11}
91. Check if a String is a Valid URL
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidURLCheck {
4 public static void main(String[] args) {
5 ```java
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a URL: ");
8 String url = scanner.nextLine();
9 boolean isValid = url.matches("^(http://|https://|ftp://|www\\.)[a-zA-Z0-9.-]+\\.[a-zA-
Z]{2,6}(/.*)?$");
10 System.out.println("The URL is " + (isValid ? "valid." : "not valid."));
11 }
12}
92. Find the Length of the Longest Word in a Sentence
java
RunCopy code
1import java.util.Scanner;
2
3public class LongestWordInSentence {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a sentence: ");
7 String sentence = scanner.nextLine();
8 String[] words = sentence.split(" ");
9 int maxLength = 0;
10 for (String word : words) {
11 if (word.length() > maxLength) {
12 maxLength = word.length();
13 }
14 }
15 System.out.println("Length of the longest word: " + maxLength);
16 }
17}
93. Check if a String is a Valid Phone Number
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidPhoneNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a phone number: ");
7 String phoneNumber = scanner.nextLine();
8 boolean isValid = phoneNumber.matches("\\d{10}");
9 System.out.println("The phone number is " + (isValid ? "valid." : "not valid."));
10 }
11}
94. Count the Number of Sentences in a Paragraph
java
RunCopy code
1import java.util.Scanner;
2
3public class CountSentences {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a paragraph: ");
7 String paragraph = scanner.nextLine();
8 String[] sentences = paragraph.split("[.!?]");
9 System.out.println("Number of sentences: " + sentences.length);
10 }
11}
95. Find the Most Frequent Word in a Sentence
java
RunCopy code
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class MostFrequentWord {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a sentence: ");
8 String sentence = scanner.nextLine();
9 String[] words = sentence.split("\\s+");
10 HashMap<String, Integer> frequencyMap = new HashMap<>();
11 for (String word : words) {
12 frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1);
13 }
14 String mostFrequent = null;
15 int maxCount = 0;
16 for (String word : frequencyMap.keySet()) {
17 if (frequencyMap.get(word) > maxCount) {
18 maxCount = frequencyMap.get(word);
19 mostFrequent = word;
20 }
21 }
22 System.out.println("Most frequent word: " + mostFrequent);
23 }
24}
96. Check if a String is a Valid IPv4 Address
java
RunCopy code
1import java.util.Scanner;
2
3public class ValidIPv4 {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter an IPv4 address: ");
7 String ip = scanner.nextLine();
8 boolean isValid = ip.matches("^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-
9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-
9]?)$");
9 System.out.println("The IPv4 address is " + (isValid ? "valid." : "not valid."));
10 }
11}
97. Check if a String is a Valid Hexadecimal Number

java
RunCopy code
1import java.util.Scanner;
2
3public class ValidHexadecimal {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a hexadecimal number: ");
7 String hex = scanner.nextLine();
8 boolean isValid = hex.matches("^[0-9A-Fa-f]+$");
9 System.out.println("The hexadecimal number is " + (isValid ? "valid." : "not valid."));
10 }
11}
98. Find the Sum of the First N Fibonacci Numbers
java
RunCopy code
1import java.util.Scanner;
2
3public class SumOfFibonacci {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int n = scanner.nextInt();
8 int sum = 0, a = 0, b = 1;
9 for (int i = 0; i < n; i++) {
10 sum += a;
11 int next = a + b;
12 a = b;
13 b = next;
14 }
15 System.out.println("Sum of first " + n + " Fibonacci numbers: " + sum);
16 }
17}
99. Check if a String is a Valid JSON
java
RunCopy code
1import java.util.Scanner;
2import com.google.gson.JsonParser;
3
4public class ValidJSON {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter a JSON string: ");
8 String jsonString = scanner.nextLine();
9 try {
10 JsonParser.parseString(jsonString);
11 System.out.println("The JSON string is valid.");
12 } catch (Exception e) {
13 System.out.println("The JSON string is not valid.");
14 }
15 }
16}
100. Find the Common Elements in Two Arrays
java
RunCopy code
1import java.util.HashSet;
2import java.util.Scanner;
3
4public class CommonElements {
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter the number of elements in first array: ");
8 int n1 = scanner.nextInt();
9 int[] arr1 = new int[n1];
10 System.out.println("Enter the elements of first array:");
11 for (int i = 0; i < n1; i++) {
12 arr1[i] = scanner.nextInt();
13 }
14 System.out.print("Enter the number of elements in second array: ");
15 int n2 = scanner.nextInt();
16 int[] arr2 = new int[n2];
17 System.out.println("Enter the elements of second array:");
18 for (int i = 0; i < n2; i++) {
19 arr2[i] = scanner.nextInt();
20 }
21 HashSet<Integer> set = new HashSet<>();
22 for (int num : arr1) {
23 set.add(num);
24 }
25 System.out.print("Common elements: ");
26 for (int num : arr2) {
27 if (set.contains(num)) {
28 System.out.print(num + " ");
29 }
30 }
31 }
32}

You might also like