Java Coding Examples
Java Coding Examples
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.");
if (isPrime(number)) {
Pa
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];
a = a + b;
b = a - b;
a = a - b;
[Link]("After swapping: a = " + a + ", b = " + b);
}
}
import [Link];
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
{
r = no%10; rev
= rev*10+r;
no=no/10;
}
}
}
Input: 15786
Output: 68751
til
Pa
g
ra
Pa
import [Link];
public class ArmstrongNumber {
til
{
a = no%10;
no = no/10;
arm =arm+a*a*a;
}
}
}
if(arm==d){
else{ Pa
[Link]("Armstrong number”);
}
import [Link];
public class NumberOfDigits {
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); }
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;
1001 is a palindrome.
til
sumOfDigits);
}
}
}
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
Strings
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](inputString);
[Link](reverseString);
}
til
Pa
g
ra
Pa
string
import [Link];
import [Link];
til
public class Main {
Pa
static void duplicateCharacterCount(String inputString) {
}
}
}
a : 4
g : 2
m : 2
n : 2
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
r : 3
til
Pa
g
ra
Pa
Character in String
import [Link];
til
public class Main {
{
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}
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;
}
[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
given string
til
import [Link];
}
permute(str, "");
Pa
public static void main(String[] args) {
String str = "abc";
}
}
Pa
abc
acb
bac
bca
cab
cba
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) {
}
Pa
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]())
{
{
if ( count !=0 )
{
return false;
}
}
return true;
Pa
}
}
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;
vowels++;
} else {
g
consonants++;
}
}
}
ra
Vowels : 3
Consonants : 7
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];
}
}
Pa
J a v A u t o m i n
import [Link];
til
[Link]("Enter a string: ");
String input = [Link]();
}
Pa
public static void printEvenIndexedCharacters(String str) {
for (int i = 0; i < [Link](); i++) {
if (i % 2 == 0) {
[Link]([Link](i));
}
}
g
}
}
ra
Atmto
import [Link];
til
[Link]("Enter a string with spaces: ");
String input = [Link]();
import [Link];
til
public static void main(String[] args) {
}
Pa
String input = [Link]();
}
return [Link]();
}
}
Pa
import [Link];
til
public class Main {
public static void main(String[] args) { Scanner
scanner = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
til
import [Link];
}
Pa
[Link]("Enter a string: ");
String input = [Link]();
+ 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]();
}
}
Output: a2b2c3d2
til
import [Link];
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);
for(char ch : [Link]())
ra
{
if([Link](ch))
{
[Link](ch);
}
else
{
Pa
[Link](ch);
}
}
[Link]("Output in lowercase: "+lowerCase);
[Link]("Output in uppercase "+upperCase);
}
til
import [Link];
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);
for(char ch : [Link]())
ra
{
if([Link](ch))
{
[Link](ch);
}
else if ([Link](ch))
Pa
{
[Link](ch);
}
}
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();
}
}
Output: 32412120000
19.)
Pa
til
Pa
g
ra
Pa
repeating characters
til
import [Link];
return maxLength;
}
}
Arrays
til
import [Link];
import [Link];
array2);
}
Pa
int[] array2 = {4, 5, 6, 7, 8};
[Link](num);
}
}
return commonSet;
}
}
til
Pa
g
ra
Pa
Arraylist
til
import [Link];
[Link]("Cherry");
[Link]("Date");
Pa
[Link]("Apple"); [Link]("Banana");
[Link]("Elderberry");
if (![Link]()) {
String firstElement = [Link](0);
String lastElement = [Link]([Link]() - 1);
}
Pa
Output:
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 + " ");
}
}
Output:
Sorted array:
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
12569
til
Pa
g
ra
Pa
import [Link];
import [Link];
til
public class Main {
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 6, 2, 5};
}
}
Pa
[Link]("Array with duplicates removed:");
for (int num : uniqueArray) {
[Link](num + " ");
return result;
}
Pa
Output:
Array with duplicates removed:
12569
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
ArrayList
til
import [Link];
import [Link];
import [Link];
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);
removed:");
for (int num : uniqueList) {
[Link](num + " ");
}
}
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
til
[Link]("The missing number is: " + missingNumber);
}
should be n+1
int arraySum = 0;
for (int num : array) {
}
arraySum += num;
}
}
g
ra
Output:
Pa
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);
empty");
Pa
public static int[] findLargestAndSmallest(int[] array) {
if (array == null || [Link] == 0) {
}
throw new IllegalArgumentException("Array must not be null or
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
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
Output:
Pa
til
String[] array = {"5", "2", "9", "a", "1", "6", "#", "3"};
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
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);
}
Pa
[Link]("Minimum value in the array: " + min);
[Link]("Maximum value in the array: " + max);
}
throw new IllegalArgumentException("Array must not be empty");
Output:
til
Pa
g
ra
Pa
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);
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
Pa
g
ra
Pa
given [ 1,1,2,2,3,4,5,5,6,6],
Output – [3,4]
til
import [Link];
import [Link];
import [Link];
import [Link];
}
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);
[Link](num, 0) + 1);
}
[Link]()) {
if ([Link]() == 1) {
[Link]([Link]());
}
}
return nonRepeatedElements;
}
}
Output :
Non-repeated elements: [3, 4]
Parag Patil | [Link]@[Link] | 7666170317 | [Link]
and equals
import [Link];
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
}
}
til
Pa
g
ra
Pa