TOP SDET JAVA Programs
1.) Java program to Find Odd or Even number
import [Link];
public class OddEven {
public static void main(String[] args) {
til
Scanner scanner = new Scanner([Link]);
[Link]("Enter any number: ");
int number = [Link]();
if (number % 2 == 0) {
}
}
} Pa
[Link](number + " is even.");
} else {
[Link](number + " is odd.");
2.) Java program to find Prime number
g
import [Link];
public class PrimeNumber {
ra
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (isPrime(number)) {
Pa
[Link](number + " is a prime number.");
} else {
[Link](number + " is not a prime number.");
}
}
public static boolean isPrime(int num) {
for (int i = 2; i <= num / 2; i++) {
//try each number by using %
if (num % i == 0) {
return false;
}
} return true;
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
3.) Java program to find Fibonacci series upto a
given number range
import [Link];
public class PrimeNumber {
public static void main(String[] args) { Scanner
sc = new Scanner([Link]);
[Link]("enter number of terms");
int number = 6;
int first = 0, second = 1, next;
[Link]("Fibonacci series is ");
for ( int i = 0; i<=number; i++)
til
{
[Link](first + "");
next = second+first;
first = second;
second = next;
}
}
Output: 0 1 1 2 3 5 8 Pa
4.) Java program to swap two numbers without
using third variable
g
import [Link];
public class SwapNumbers {
ra
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number: ");
int a = 5,
[Link]("Enter the second number: ");
int b = 10;
Pa
[Link]("Before swapping: a = " + a + ", b = " + b);
a = a + b;
b = a - b;
a = a - b;
[Link]("After swapping: a = " + a + ", b = " + b);
}
}
Output: After Swapping: a = 10 , b = 5
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
5.) Java program to Find Factorial on given Number
import [Link];
public class FactorialNumber {
public
static void
main(String[
] args) {
int
factorial
til
=1;
Scanner
scanner = new
Scanner(Syste
[Link]);
Input: 5!
Pa }
[Link].p
rint("Enter
any number
"); int
number = 5;
for (int i =
1; i <=
number;
i++){
g
factorial =
factorial *
i;
}
ra
[Link]("Factorial
number is :" +factorial);
}
Output: 5! = 5*4*3*2*1 = 120
Pa
6.) Java program to Reverse Number
import [Link];
public class ReverseNumber {
public static void main(String[] args) {
int no, rev=0,r,a;
Scanner scanner = new Scanner([Link]);
[Link]("Enter any number : ");
no = [Link]();
a = no;
while(no>0)
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
{
r = no%10; rev
= rev*10+r;
no=no/10;
}
[Link]("Reverse : " +rev);
}
}
Input: 15786
Output: 68751
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
7.) Java program to find Armstrong Number
import [Link];
public class ArmstrongNumber {
public static void main(String[] args) {
int arm=0, a,b,c,d,no;
Scanner scanner = new Scanner([Link]);
[Link]("Enter any number : ");
no = [Link]();
d = no;
while(no>0)
til
{
a = no%10;
no = no/10;
arm =arm+a*a*a;
}
}
}
if(arm==d){
else{ Pa
[Link]("Armstrong number”);
}
[Link]("Not Armstrong number”);
}
g
8.) Java program to find number of digits in given
number
ra
import [Link];
public class NumberOfDigits {
public static void main(String[] args) {
Pa
int no = 0, a = 0;
Scanner scanner = new Scanner([Link]);
[Link]("Enter any number : ");
no = [Link]();
if(no<0)
{
no = no * -1;
} else if (no==0) {
no=1;
}
while(no>0)
{
no=no/10;
a++;}
[Link]("Number of digits in given number is :" +a); }
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
9.) Java program to find Palindrome number
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();
if (isPalindrome(number)) {
[Link](number + " is a palindrome.");
til
} else {
[Link](number + " is not a palindrome.");
}
}
int reversedNumber = 0;
while (num != 0) {
int digit = num % 10;
Pa
public static boolean isPalindrome(int num) {
int originalNumber = num;
reversedNumber = reversedNumber * 10 + digit;
num = num/10;
}
g
return originalNumber == reversedNumber;
}
}
ra
Enter a number: 1001
Pa
1001 is a palindrome.
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
10.)Java program to calculate the sum of digits of
a number
public class Main {
public static void main(String[] args) {
int number = 12345;
int sumOfDigits = calculateSumOfDigits(number);
[Link]("Sum of digits of " + number + " is: " +
til
sumOfDigits);
}
public static int calculateSumOfDigits(int number) {
int sum = 0;
}
}
while (number > 0) {
} Pa
int digit = number % 10; // Extract the last digit
sum = sum + digit; // Add the digit to sum
number = number / 10; // Remove the last digit from number
return sum;
g
ra
Output:
Sum of digits of 12345 is: 15
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Strings
1.)Java program to reverse a string
import [Link];
public class Test {
public static void main(String[] args) {
til
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
char ch;
String nstr = "";
}
nstr = ch + nstr;
Pa
for (int i = 0; i < [Link](); i++) {
ch = [Link](i);
[Link]("Reversed String is : " + nstr);
2.)Java program to reverse each word of a given
string
g
public static void main(String[] args) {
reverseEachWordOfString("Java is good programming langauges");
ra
static void reverseEachWordOfString(String inputString)
{
String[] words = [Link](" ");
Pa
String reverseString = "";
for (int i = 0; i < [Link]; i++) {
String word = words[i];
String nstr = "";
char ch;
for (int j = 0; j < [Link](); j++) {
ch = [Link](j);
nstr = ch + nstr;
}
reverseString = reverseString + nstr + " ";
}
[Link](inputString);
[Link](reverseString);
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Input: Java is good programming langauges
Output: avaJ si doog gnimmargorp seguagnal
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
3.)Java program to find duplicate characters in a
string
import [Link];
import [Link];
til
public class Main {
public static void main(String[] args) {
duplicateCharacterCount("Learn Java Programming");
}
Pa
static void duplicateCharacterCount(String inputString) {
HashMap<Character, Integer> charCountMap = new HashMap<>();
char[] strArray = [Link]();
for (char c : strArray) {
if ([Link](c)) {
[Link](c, [Link](c) + 1);
} else {
[Link](c, 1);
g
}
}
Set<Character> charsInString = [Link]();
ra
[Link]("Duplicate Characters in : " + inputString);
for (Character ch : charsInString) {
if ([Link](ch) > 1) {
[Link](ch + " : " + [Link](ch));
}
Pa
}
}
}
Duplicate Characters in : Learn Java Programming
a : 4
g : 2
m : 2
n : 2
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
r : 3
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
4.)Java program to count Occurrences of Each
Character in String
import [Link];
til
public class Main {
public static void main(String[] args) {
CharacterCount("Test Automation Java Automation");
}
{
Pa
static void CharacterCount(String inputString) {
HashMap<String,Integer> charCountMap = new HashMap<>();
for(String s : [Link](" "))
if([Link](s))
{
}
[Link](s,[Link](s)+1);
else
{
g
[Link](s,1);
}
}
[Link]("Count of Characters in a given string : " +
ra
charCountMap);
}
}
Count of Characters in a given string : {Java=1, Automation=2, Test=1}
5.)Java program to count the number of words in
Pa
a string
public class Main {
public static void main(String[] args) {
[Link]("Enter the String");
Scanner sc = new Scanner([Link]); String
s = [Link]();
int count = 1;
for (int i = 0; i < [Link]() - 1; i++) {
if (([Link](i) == ' ') && ([Link](i + 1) != ' ')) {
count++;
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
}
[Link]("Number of words in a string: " +count); }
}
Enter the String: Welcome to Java World Number
of words in a string: 4
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
6.)Java program to find all permutations of a
given string
til
import [Link];
public class Main {
}
permute(str, "");
Pa
public static void main(String[] args) {
String str = "abc";
static void permute(String str, String prefix) {
if ([Link]() == 0) {
[Link](prefix);
} else {
for (int i = 0; i < [Link](); i++) {
g
String rem = [Link](0,i) + [Link](i+1);
permute(rem,prefix + [Link](i));
}
}
ra
}
}
Pa
abc
acb
bac
bca
cab
cba
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
7.)Java program to find if a string is Palindrome
import [Link];
til
public class Main {
public static void main(String[] args) {
String str = "madam";
[Link](isPalindrome(str));
}
int start = 0;
Pa
static boolean isPalindrome(String str) {
int end = [Link]() - 1;
while (start < end) {
if ([Link](start) != [Link](end)) {
return false;
}
start++;
g
end--;
}
return true;
}
ra
}
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
8.)Java program to determine if Two Strings are
Anagrams
public class Main {
til
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
[Link](areAnagrams(str1,str2));
}
}
return false;
Pa
static boolean areAnagrams(String str1, String str2) {
if([Link]() != [Link]())
{
int[] charCount = new int[256];
for( int i = 0; i < [Link](); i++)
{
charCount[[Link](i)]++;
g
charCount[[Link](i)]--;
}
for ( int count : charCount)
ra
{
if ( count !=0 )
{
return false;
}
}
return true;
Pa
}
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
9.)Java program to Count Vowels and Consonants
in a given string
til
public class Main {
public static void main(String[] args) {
String str = "Hello World";
VowelConsonantCount(str);
{
}
str = [Link]();
Pa
static void VowelConsonantCount(String str) {
int vowels = 0, consonants = 0;
for (char c : [Link]()) {
if (c >= 'a' && c <= 'z') {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowels++;
} else {
g
consonants++;
}
}
}
ra
[Link]("Vowels : " + vowels);
[Link]("Consonants : " + consonants);
}
}
Pa
Vowels : 3
Consonants : 7
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
10.) Java program to print unqiue characters
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
til
[Link]("Unique characters in \"" + input + "\":");
printUniqueCharacters(input);
Pa
public static void printUniqueCharacters(String str) {
// Assume ASCII characters (0-127), use boolean array to track
character occurrences
boolean[] unique = new boolean[128];
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if (!unique[ch]) {
g
unique[ch] = true;
[Link](ch + " ");
}
}
ra
}
}
Pa
Enter a string: Java Automation
Unique characters in "Java Automation":
J a v A u t o m i n
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
11.) Java program to print even indexed
characters
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
til
[Link]("Enter a string: ");
String input = [Link]();
[Link]("Even indexed characters in \"" + input + "\":");
printEvenIndexedCharacters(input);
}
Pa
public static void printEvenIndexedCharacters(String str) {
for (int i = 0; i < [Link](); i++) {
if (i % 2 == 0) {
[Link]([Link](i));
}
}
g
}
}
ra
Enter a string: Automation
Pa
Even indexed characters in "Automation":
Atmto
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
12.) Java program to remove space from a
given string
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
til
[Link]("Enter a string with spaces: ");
String input = [Link]();
String stringWithoutSpaces = removeSpaces(input);
[Link]("String without spaces: " +
stringWithoutSpaces);
}
Pa
public static String removeSpaces(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
if ([Link](i) != ' ') {
[Link]([Link](i));
}
}
return [Link]();
g
}
}
ra
Enter a string with spaces: Welcome to Java World
String without spaces: WelcometoJavaWorld
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
13.) Java program to print each letter twice
from a given string
import [Link];
public class Main {
til
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
}
Pa
String input = [Link]();
String doubledString = doubleCharacters(input);
[Link]("Doubled characters: " + doubledString);
public static String doubleCharacters(String str) {
StringBuilder doubled
= new StringBuilder();
for (int i = 0; i <
g
[Link](); i++) {
twice char ch = [Link](i);
[Link](ch).append(ch);
// Append each character
ra
}
return [Link]();
}
}
Pa
Enter a string: hello
Doubled characters: hheelllloo
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
14.) Java program to swap two string without
using 3rd variable
import [Link];
til
public class Main {
public static void main(String[] args) { Scanner
scanner = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
str2 = " + str2);
Pa
[Link]("Enter second string: ");
String str2 = [Link]();
[Link]("Before swapping: str1 = " + str1 + ",
// Swapping without using a third variable
str1 = str1 + str2; // Concatenate str1 and str2 and
store in str1
str2 = [Link](0, [Link]() - [Link]());
// Extract the initial part (original str1) from the concatenated
g
string
str1 = [Link]([Link]()); // Extract the
remaining part (original str2) from the concatenated string
ra
[Link]("After swapping: str1 = " + str1 + ",
str2 = " + str2);
}
}
Pa
Enter first string: Hello
Enter second string: World
Before swapping: str1 = Hello, str2 = World
After swapping: str1 = World, str2 = Hello
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
15.) Java program to gives Output: a2b2c3d2
for the Input String Str = “aabbcccdd”
til
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
}
Pa
[Link]("Enter a string: ");
String input = [Link]();
String output = getCharacterCount(input);
[Link]("Output: " + output);
public static String getCharacterCount(String str) {
StringBuilder result = new StringBuilder();
int count = 1;
g
for (int i = 0; i < [Link](); i++) {
// If the next character is the same, increase the count
if (i + 1 < [Link]() && [Link](i) == [Link](i
ra
+ 1)) { count++;
} else {
// Append the character and its
count to the result
[Link]([Link]
rAt(i)).append(count)
Pa
; count = 1; //
Reset the count
}
}
return [Link]();
}
}
Enter a string: aabbcccdd
Output: a2b2c3d2
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
16.) Java program to gives two Output:
“abcde”, “ABCDE” for the Input
String Str = “aBACbcEDed”
til
import [Link];
public class Main {
Pa
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
}
[Link]("Original String is: "+ input);
separateCharacters(input);
public static void separateCharacters(String input)
{
g
StringBuilder lowerCase = new StringBuilder();
StringBuilder upperCase = new StringBuilder();
for(char ch : [Link]())
ra
{
if([Link](ch))
{
[Link](ch);
}
else
{
Pa
[Link](ch);
}
}
[Link]("Output in lowercase: "+lowerCase);
[Link]("Output in uppercase "+upperCase);
}
Enter a string: aBACbcEDed
Output in lowercase: abced
Output in uppercase: ABCED
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
17.) Java program to gives two Output:
“Subburaj”, “123” for the Input
String Str = “Subbu123raj”
til
import [Link];
public class Main {
Pa
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
}
[Link]("Original String is: "+ input);
separateAplhaAndNumeric(input);
public static void separateAlphaAndNumeric(String input)
{
g
StringBuilder alphaPart = new StringBuilder();
StringBuilder numericPart = new StringBuilder();
for(char ch : [Link]())
ra
{
if([Link](ch))
{
[Link](ch);
}
else if ([Link](ch))
Pa
{
[Link](ch);
}
}
[Link]("Output in Alpha: "+[Link]());
[Link]("Output in Numeric:
"+[Link]());
}
Enter a string: Subbu123raj
Output in lowercase: Subburaj
Output in uppercase: 123
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
18.) Java program to gives Output:
“32412120000” for the Input
String Str = “32400121200”
public class Main {
public static void main(String[] args) {
til
String input = "32400121200";
String output = rearrangeDigits(input);
[Link]("Output: " + output);
}
Pa
public static String rearrangeDigits(String input) {
// Split the input into parts: digits and non-digits
StringBuilder digits = new StringBuilder();
StringBuilder nonDigits = new StringBuilder();
for (char c : [Link]()) {
if ([Link](c)) {
[Link](c);
} else {
[Link](c);
}
g
}
// Concatenate non-digits followed by digits
return [Link]() + [Link]();
ra
}
}
Output: 32412120000
19.)
Pa
Java program to gives Output:
“00003241212” for the Input
String Str = “32400121200”
public class Main {
public static void main(String[] args) {
String input = "32400121200";
String formattedOutput = [Link]("%011d",
[Link](input));
[Link]("Formatted output: " + formattedOutput);
}
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Formatted output: 00003241212
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
20.) Java program to find the longest without
repeating characters
til
import [Link];
public class Main {
public static
String s1
String s2
String s3
String s4
Pa
void main(String[] args) {
= "abcabcbb"; // Expected: "abc", length 3
= "bbbbb";
= "pwwkew";
= "";
// Expected: "b", length 1
// Expected: "wke", length 3
// Expected: "", length 0
[Link]("Longest substring without repeating
characters in s1: " + lengthOfLongestSubstring(s1)); // Output:
[Link]("Longest substring without repeating
3
characters in s2: " + lengthOfLongestSubstring(s2)); // Output: 1
[Link]("Longest substring without repeating
g
characters in s3: " + lengthOfLongestSubstring(s3)); // Output: 3
[Link]("Longest substring without repeating
characters in s4: " + lengthOfLongestSubstring(s4)); // Output: 0
ra
public static int lengthOfLongestSubstring(String s) {
HashSet<Character> set = new HashSet<>();
int maxLength = 0;
int start = 0; int
end = 0;
Pa
while (end < [Link]()) {
char currentChar = [Link](end);
if () {
[Link](currentChar);
maxLength = [Link](maxLength, end - start + 1);
end++;
} else {
[Link]([Link](start));
start++;
}
}
return maxLength;
}
}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Arrays
1.)Find common elements between
two arrays
til
import [Link];
import [Link];
public class CommonElements {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
array2);
}
Pa
int[] array2 = {4, 5, 6, 7, 8};
Set<Integer> commonElements = findCommonElements(array1,
[Link]("Common elements: " + commonElements);
public static Set<Integer> findCommonElements(int[] array1,
int[] array2) {
g
Set<Integer> set1 = new HashSet<>();
Set<Integer> commonSet = new HashSet<>();
// Add elements of the first array to the set
ra
for (int num : array1) {
[Link](num);
}
// Check for common elements in the second array
for (int num : array2) {
if ([Link](num)) {
Pa
[Link](num);
}
}
return commonSet;
}
}
Input: array1 = {1,2,3,4,5} and
array2 = {4,5,6,7,8}
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Output: Common elements: [4, 5]
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
2.)Find first and last element of
Arraylist
til
import [Link];
public class Main {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
[Link]("Cherry");
[Link]("Date");
Pa
[Link]("Apple"); [Link]("Banana");
[Link]("Elderberry");
if (![Link]()) {
String firstElement = [Link](0);
String lastElement = [Link]([Link]() - 1);
[Link]("First element: " + firstElement);
g
[Link]("Last element: " + lastElement);
} else {
[Link]("The ArrayList is empty.");
}
}
ra
}
Pa
Output:
First element: Apple
Last element: Elderberry
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
3.)Sort an array without using in-built
method
til
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6};
selectionSort(array);
}
} Pa
[Link]("Sorted array:");
for (int num : array) {
[Link](num + " ");
public static void selectionSort(int[] array) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
g
for (int j = i + 1; j < n; j++) { if
(array[j] < array[minIndex]) {
minIndex = j;
}
ra
// Swap array[i] and array[minIndex]
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
Pa
}
}
Output:
Sorted array:
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
12569
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
4.)Remove duplicates from an Array
import [Link];
import [Link];
til
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 2, 5};
int[] uniqueArray = removeDuplicates(array);
}
}
Pa
[Link]("Array with duplicates removed:");
for (int num : uniqueArray) {
[Link](num + " ");
public static int[] removeDuplicates(int[] array) {
Set<Integer> set = new HashSet<>();
for (int num : array) {
[Link](num);
g
}
int[] result = new int[[Link]()];
int i = 0;
ra
for (int num : set) {
result[i++] = num;
}
return result;
}
Pa
Output:
Array with duplicates removed:
12569
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
5.)Remove duplicates from an
ArrayList
til
import [Link];
import [Link];
import [Link];
public class Main {
Pa
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
[Link](5);
[Link](2);
[Link](9);
[Link](1);
[Link](6);
[Link](2);
[Link](5);
g
ArrayList<Integer> uniqueList =
removeDuplicates(arrayList);
[Link]("ArrayList with duplicates
ra
removed:");
for (int num : uniqueList) {
[Link](num + " ");
}
}
public static ArrayList<Integer>
Pa
removeDuplicates(ArrayList<Integer> list) {
Set<Integer> set = new HashSet<>(list);
return new ArrayList<>(set);
}
}
Output:
ArrayList with duplicates removed:
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
12569
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
6.)Find the missing number in an Array
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 4, 5, 6}; // Missing number is 3
int missingNumber = findMissingNumber(array);
til
[Link]("The missing number is: " + missingNumber);
}
public static int findMissingNumber(int[] array) {
should be n+1
int arraySum = 0;
for (int num : array) {
}
arraySum += num;
return totalSum - arraySum;
Pa
int n = [Link] + 1; // Since one number is missing, the length
int totalSum = n * (n + 1) / 2; // Sum of first n natural numbers
}
}
g
ra
Output:
Pa
The missing number is: 3
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
7.)Find the largest and smallest
element in an Array
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 3};
til
int[] result = findLargestAndSmallest(array);
[Link]("Smallest element: " + result[0]);
[Link]("Largest element: " + result[1]);
}
empty");
Pa
public static int[] findLargestAndSmallest(int[] array) {
if (array == null || [Link] == 0) {
}
throw new IllegalArgumentException("Array must not be null or
int smallest = array[0];
g
int largest = array[0];
for (int num : array) {
if (num < smallest) {
ra
smallest = num;
}
if (num > largest) {
largest = num;
}
Pa
}
return new int[]{smallest, largest};
}
}
Output:
Smallest element: 1
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Largest element: 9
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
8.)Search element in an Array
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 3};
int target = 6;
int index = linearSearch(array, target);
til
if (index != -1) {
[Link]("Element " + target + " found at index: " +
index);
} else {
[Link]("Element " + target + " not found in the
array.");
}
}
Pa
public static int linearSearch(int[] array, int target) {
for (int i = 0; i < [Link]; i++) {
}
if (array[i] == target) {
}
return i; // Element found, return index
return -1; // Element not found
}
g
}
ra
Output:
Pa
Element 6 found at index: 4
Element 10 not found in the array
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
9.)Array consists of integers and special
characters,sum only integers
public class Main {
public static void main(String[] args) {
til
String[] array = {"5", "2", "9", "a", "1", "6", "#", "3"};
int sum = sumIntegers(array);
[Link]("Sum of integers in the array: " + sum);
}
Pa
public static int sumIntegers(String[] array) {
int sum = 0;
for (String element : array) {
try {
int num = [Link](element);
sum += num;
} catch (NumberFormatException e) {
// Ignore non-integer elements
g
}
}
return sum;
}
ra
Output:
Pa
Sum of integers in the array: 26
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
10.)Find Minimum and Maximum
from an Array
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 3};
til
// Find maximum and minimum
int max = findMaximum(array);
int min = findMinimum(array);
// Print the results
}
Pa
[Link]("Minimum value in the array: " + min);
[Link]("Maximum value in the array: " + max);
public static int findMaximum(int[] array) {
if ([Link] == 0) {
}
throw new IllegalArgumentException("Array must not be empty");
int max = array[0]; // Initialize max to the first element
for (int i = 1; i < [Link]; i++) {
if (array[i] > max) {
g
max = array[i]; // Update max if current element is larger
}
}
return max;
ra
public static int findMinimum(int[] array) {
if ([Link] == 0) {
throw new IllegalArgumentException("Array must not be empty");
}
int min = array[0]; // Initialize min to the first element
Pa
for (int i = 1; i < [Link]; i++) {
if (array[i] < min) {
min = array[i]; // Update min if current element is smaller
}
}
return min; }
}
Output:
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Minimum value in the array: 1
Maximum value in the array: 9
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
11.)Java program to count Odd and
Even number from given array
Input: {1,2,3,4,5,6,7,8,9}
til
public class Main {
public static void main(String[] args) { int[]
array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
}
Pa
int[] count = countOddAndEven(array);
[Link]("Even numbers count: " + count[1]);
[Link]("Odd numbers count: " + count[0]);
public static int[] countOddAndEven(int[] array) {
int[] count = new int[2]; // Index 0 for odd count, Index 1 for
even count
g
f
o
r
(
ra
i
n
t
n
u
m
:
Pa
a
r
r
a
y
)
{
i
f
(
n
u
m
%
2
=
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
= count
0 } else {
) count[0]++; // Increment odd
{ count
count[1]++; // Increment even
}
}
return count;
}
}
Output:
til
Even numbers count: 4
Odd numbers count: 5
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
12.)Java program – input array was
given [ 1,1,2,2,3,4,5,5,6,6],
Output – [3,4]
til
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
}
Pa
public static void main(String[] args) {
int[] array = {1, 1, 2, 2, 3, 4, 5, 5, 6, 6};
List<Integer> result = findNonRepeatedElements(array);
[Link]("Non-repeated elements: " + result);
public static List<Integer> findNonRepeatedElements(int[]
array) {
// Step 1: Count occurrences of each element using a
g
HashMap Map<Integer, Integer> countMap =
new HashMap<>(); for (int num :
array) {
[Link](num,
ra
[Link](num, 0) + 1);
}
// Step 2: Identify elements with count equal to 1 (non-
repeated)
List<Integer> nonRepeatedElements = new ArrayList<>();
for ([Link]<Integer, Integer> entry :
Pa
[Link]()) {
if ([Link]() == 1) {
[Link]([Link]());
}
}
return nonRepeatedElements;
}
}
Output :
Non-repeated elements: [3, 4]
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
Java program to implement hashcode
and equals
import [Link];
public class Student {
private int id;
til
private String name;
// Constructor
public Student(int id, String name) {
[Link] = id;
}
[Link] = name;
// hashCode method
@Override
public int hashCode() {
return [Link](id, name);
Pa
// Getters and setters (omitted for brevity)
}
g
// equals method
@Override
public boolean equals(Object obj) {
if (this == obj)
ra
return true;
if (obj == null || getClass() != [Link]())
return false;
Student student = (Student) obj;
return id == [Link] && [Link](name, [Link]);
}
Pa
public static void main(String[] args) {
// Creating objects of Student class
Student student1 = new Student(1, "Alice");
Student student2 = new Student(2, "Bob");
Student student3 = new Student(1, "Alice");
// Testing equals method
[Link]("[Link](student2): " +
[Link](student2)); // Output: false
[Link]("[Link](student3): " +
[Link](student3)); // Output: true
// Testing hashCode method
[Link]("Hashcode of student1: " + [Link]());
[Link]("Hashcode of student2: " + [Link]());
[Link]("Hashcode of student3: " + [Link]());
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
}
}
til
Pa
g
ra
Pa
Parag Patil | [Link]@[Link] | 7666170317 | [Link]