Unique Java Programs for Exam Preparation
1. Reverse a Number
Reverse the digits of a given number.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
sc.close();
}
}
2. Find the Second Largest Element in an Array
Determine the second largest element in a single-dimensional array.
import java.util.Scanner;
import java.util.Arrays;
public class SecondLargest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int secondLargest = arr[arr.length - 2];
System.out.println("Second Largest Element: " + secondLargest);
sc.close();
}
}
3. Check if a Number is Palindrome
Determine whether a given number is a palindrome.
import java.util.Scanner;
public class PalindromeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int original = num, reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (original == reversed) {
System.out.println("The number is a Palindrome.");
} else {
System.out.println("The number is not a Palindrome.");
}
sc.close();
}
}
4. Count Frequency of Each Element in an Array
Count how many times each element appears in a single-dimensional array.
import java.util.Scanner;
public class FrequencyCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
int[] freq = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
freq[i] = -1;
}
for (int i = 0; i < size; i++) {
int count = 1;
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
if (freq[i] != 0) {
freq[i] = count;
}
}
System.out.println("Element | Frequency");
for (int i = 0; i < size; i++) {
if (freq[i] != 0) {
System.out.println(" " + arr[i] + " | " + freq[i]);
}
}
sc.close();
}
}
5. Sum of Digits of a Number
Calculate the sum of digits of a given number.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits: " + sum);
sc.close();
}
}
6. Check if Array is Sorted
Determine whether a single-dimensional array is sorted in ascending order.
import java.util.Scanner;
public class ArraySortedCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
boolean isSorted = true;
for (int i = 0; i < size - 1; i++) {
if (arr[i] > arr[i + 1]) {
isSorted = false;
break;
}
}
if (isSorted) {
System.out.println("The array is sorted.");
} else {
System.out.println("The array is not sorted.");
}
sc.close();
}
}
Check if a String is a Panagram
import java.util.Scanner;
public class PangramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();
boolean[] alphabet = new boolean[26];
int index;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
index = str.charAt(i) - 'a';
alphabet[index] = true;
boolean isPangram = true;
for (boolean b : alphabet) {
if (!b) {
isPangram = false;
break;
}
if (isPangram) {
System.out.println("The string is a Pangram.");
} else {
System.out.println("The string is not a Pangram.");
sc.close();
2. Find the Missing Number in an Array
Description:
Given an array of n-1 integers in the range 1 to n, find the missing number.
Code:
java
CopyEdit
import java.util.Scanner;
public class MissingNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array (n): ");
int n = sc.nextInt();
int[] arr = new int[n - 1];
int total = n * (n + 1) / 2;
System.out.println("Enter " + (n - 1) + " elements:");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
total -= arr[i];
System.out.println("The missing number is: " + total);
sc.close();
3. Check if Two Strings are Anagrams
Description:
Determine whether two given strings are anagrams of each other.
Code:
java
CopyEdit
import java.util.Arrays;
import java.util.Scanner;
public class AnagramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = sc.nextLine();
System.out.print("Enter the second string: ");
String str2 = sc.nextLine();
if (str1.length() != str2.length()) {
System.out.println("The strings are not Anagrams.");
return;
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
if (Arrays.equals(arr1, arr2)) {
System.out.println("The strings are Anagrams.");
} else {
System.out.println("The strings are not Anagrams.");
sc.close();
4. Rotate an Array by K Positions
Description:
Shift elements in an array to the right by K positions.
Code:
java
CopyEdit
import java.util.Scanner;
public class RotateArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
System.out.print("Enter the value of K: ");
int k = sc.nextInt();
k %= n;
System.out.println("Array after rotation:");
for (int i = n - k; i < n; i++) {
System.out.print(arr[i] + " ");
for (int i = 0; i < n - k; i++) {
System.out.print(arr[i] + " ");
sc.close();
}
5. Print All Subarrays of an Array
Description:
Print all possible subarrays of a given array.
Code:
java
CopyEdit
import java.util.Scanner;
public class Subarrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
System.out.println("All subarrays:");
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = i; k <= j; k++) {
System.out.print(arr[k] + " ");
System.out.println();
sc.close();