Unique_Java_Programs_Exam_Preparation
Unique_Java_Programs_Exam_Preparation
1. Reverse a Number
Reverse the digits of a given number.
import java.util.Scanner;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
sc.close();
}
}
import java.util.Scanner;
import java.util.Arrays;
import java.util.Scanner;
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();
}
}
import java.util.Scanner;
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();
}
}
import java.util.Scanner;
import java.util.Scanner;
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;
int index;
alphabet[index] = true;
if (!b) {
isPangram = false;
break;
}
if (isPangram) {
} else {
sc.close();
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;
int n = sc.nextInt();
int total = n * (n + 1) / 2;
total -= arr[i];
sc.close();
Description:
Determine whether two given strings are anagrams of each other.
Code:
java
CopyEdit
import java.util.Arrays;
import java.util.Scanner;
return;
Arrays.sort(arr1);
Arrays.sort(arr2);
if (Arrays.equals(arr1, arr2)) {
} else {
sc.close();
Description:
Shift elements in an array to the right by K positions.
Code:
java
CopyEdit
import java.util.Scanner;
int n = sc.nextInt();
arr[i] = sc.nextInt();
int k = sc.nextInt();
k %= n;
sc.close();
}
Description:
Print all possible subarrays of a given array.
Code:
java
CopyEdit
import java.util.Scanner;
int n = sc.nextInt();
arr[i] = sc.nextInt();
System.out.println("All subarrays:");
System.out.println();
sc.close();