0% found this document useful (0 votes)
20 views61 pages

Java Practice

The document contains a series of Java programs that demonstrate various programming concepts and functionalities. Each program is designed to perform a specific task, such as printing 'Hello, World!', adding two numbers, checking for even or odd numbers, and more complex operations like sorting, searching, and matrix manipulation. The document serves as a comprehensive reference for beginners to understand basic to intermediate programming techniques in Java.
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)
20 views61 pages

Java Practice

The document contains a series of Java programs that demonstrate various programming concepts and functionalities. Each program is designed to perform a specific task, such as printing 'Hello, World!', adding two numbers, checking for even or odd numbers, and more complex operations like sorting, searching, and matrix manipulation. The document serves as a comprehensive reference for beginners to understand basic to intermediate programming techniques in Java.
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

// Program 1: Hello World

class Program1 {

public static void main(String[] args) {

[Link]("Hello, World!");

// Program 2: Add Two Numbers

class Program2 {

public static void main(String[] args) {

int a = 10, b = 20;

[Link]("Sum: " + (a + b));

// Program 3: Check Even or Odd

class Program3 {

public static void main(String[] args) {

int num = 7;

[Link](num + " is " + (num % 2 == 0 ? "Even" : "Odd"));

// Program 4: Find Largest of Two Numbers

class Program4 {

public static void main(String[] args) {

int a = 5, b = 10;

[Link]("Largest: " + (a > b ? a : b));

}
}

// Program 5: Swap Two Numbers Without Temp Variable

class Program5 {

public static void main(String[] args) {

int a = 3, b = 8;

[Link]("Before Swap: a = " + a + ", b = " + b);

a = a + b;

b = a - b;

a = a - b;

[Link]("After Swap: a = " + a + ", b = " + b);

// Program 6: Find Factorial

class Program6 {

public static void main(String[] args) {

int num = 5, factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

[Link]("Factorial of " + num + ": " + factorial);

// Program 7: Generate Fibonacci Series

class Program7 {

public static void main(String[] args) {

int n = 10, a = 0, b = 1;

[Link]("Fibonacci: " + a + " " + b);

for (int i = 2; i < n; i++) {


int next = a + b;//0+1 next = 1 ,1+1 ,+3

[Link](" " + next);

a = b; //a = 1 ,a =1

b = next;// b = 1,b = 2

// Program 8: Check Prime Number

class Program8 {

public static void main(String[] args) {

int num = 29;

boolean isPrime = true;

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) {

isPrime = false;

break;

[Link](num + " is " + (isPrime ? "a Prime Number" : "not a Prime Number"));

// Program 9: Reverse a Number

class Program9 {

public static void main(String[] args) {

int num = 12345, reversed = 0;

while (num != 0) {

int digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;
}

[Link]("Reversed Number: " + reversed);

// Program 10: Check Palindrome Number

class Program10 {

public static void main(String[] args) {

int num = 121, reversed = 0, original = num;

while (num != 0) {

int digit = num % 10;

reversed = reversed * 10 + digit;

num /= 10;

[Link](original + (original == reversed ? " is a Palindrome" : " is not a Palindrome"));

// (Continue adding more programs with distinct functionalities)

// Program 11: Count Vowels in a String

class Program11 {

public static void main(String[] args) {

String str = "Hello World";

int count = 0;

for (char c : [Link]().toCharArray()) {

if ("aeiou".indexOf(c) != -1) {

count++;

[Link]("Number of Vowels: " + count);


}

// Program 12: Find GCD of Two Numbers

class Program12 {

public static void main(String[] args) {

int a = 56, b = 98;

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

[Link]("GCD: " + a);

// Program 13: Check Leap Year

class Program13 {

public static void main(String[] args) {

int year = 2024;

[Link](year + " is " + ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? "a
Leap Year" : "not a Leap Year"));

// Add Programs 14 to 100

// Example types: Sorting (Bubble, Merge), Searching (Binary, Linear), Matrix Operations, Recursion,
OOP (Classes, Polymorphism, Inheritance), File Handling, Threads, Design Patterns, Algorithms
(Dijkstra, BFS/DFS), etc.
// Program 14: Find LCM of Two Numbers

class Program14 {

public static void main(String[] args) {

int a = 15, b = 20, lcm;

lcm = (a > b) ? a : b;

while (true) {

if (lcm % a == 0 && lcm % b == 0) {

[Link]("LCM: " + lcm);

break;

lcm++;

// Program 15: Transpose of a Matrix

class Program15 {

public static void main(String[] args) {

int[][] matrix = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

int[][] transpose = new int[3][3];

for (int i = 0; i < [Link]; i++) {

for (int j = 0; j < matrix[i].length; j++) {

transpose[j][i] = matrix[i][j];

}
[Link]("Transpose:");

for (int[] row : transpose) {

for (int element : row) {

[Link](element + " ");

[Link]();

// Program 16: Linear Search

class Program16 {

public static void main(String[] args) {

int[] arr = {5, 3, 8, 6, 2};

int target = 6, index = -1;

for (int i = 0; i < [Link]; i++) {

if (arr[i] == target) {

index = i;

break;

[Link]("Element " + target + " found at index: " + index);

// Program 17: Bubble Sort

class Program17 {

public static void main(String[] args) {

int[] arr = {5, 1, 4, 2, 8};

for (int i = 0; i < [Link] - 1; i++) {

for (int j = 0; j < [Link] - i - 1; j++) {


if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

// Program 18: Binary Search

class Program18 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int target = 3, low = 0, high = [Link] - 1, mid, index = -1;

while (low <= high) {

mid = (low + high) / 2;

if (arr[mid] == target) {

index = mid;

break;

} else if (arr[mid] < target) {

low = mid + 1;

} else {

high = mid - 1;

[Link]("Element " + target + " found at index: " + index);


}

// Program 19: Reverse a String

class Program19 {

public static void main(String[] args) {

String str = "Hello";

String reversed = new StringBuilder(str).reverse().toString();

[Link]("Reversed String: " + reversed);

// Program 20: Count Words in a Sentence

class Program20 {

public static void main(String[] args) {

String sentence = "This is a sample sentence";

int wordCount = [Link](" ").length;

[Link]("Word Count: " + wordCount);

// Program 21: Check Palindrome String

class Program21 {

public static void main(String[] args) {

String str = "radar";

String reversed = new StringBuilder(str).reverse().toString();

[Link](str + " is " + ([Link](reversed) ? "a Palindrome" : "not a Palindrome"));

// Program 22: Calculate Power Using Recursion


class Program22 {

public static void main(String[] args) {

int base = 2, exponent = 3;

[Link]("Result: " + power(base, exponent));

static int power(int base, int exp) {

if (exp == 0) return 1;

return base * power(base, exp - 1);

// Program 23: Merge Two Arrays

class Program23 {

public static void main(String[] args) {

int[] arr1 = {1, 2, 3};

int[] arr2 = {4, 5, 6};

int[] merged = new int[[Link] + [Link]];

[Link](arr1, 0, merged, 0, [Link]);

[Link](arr2, 0, merged, [Link], [Link]);

[Link]("Merged Array:");

for (int num : merged) {

[Link](num + " ");

// (Continue for Programs 24 to 100)

// Examples could include:

// - Advanced Sorting: Merge Sort, Quick Sort

// - Matrix Operations: Addition, Multiplication


// - Algorithms: Dijkstra's, BFS/DFS

// - File Handling: Read/Write to a File

// - Object-Oriented Programming: Inheritance, Polymorphism

// - Multithreading: Synchronization, Deadlock Handling

// - Design Patterns: Singleton, Factory, Observer

// - Advanced Java Concepts: Streams API, Lambda Expressions, Functional Interfaces

// Program 24: Sum of Elements in an Array

class Program24 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int sum = 0;

for (int num : arr) {

sum += num;

[Link]("Sum of Elements: " + sum);

// Program 25: Find the Largest Element in an Array

class Program25 {

public static void main(String[] args) {

int[] arr = {5, 2, 9, 1, 6};

int max = arr[0];

for (int num : arr) {

if (num > max) {

max = num;

[Link]("Largest Element: " + max);


}

// Program 26: Matrix Addition

class Program26 {

public static void main(String[] args) {

int[][] mat1 = {{1, 2}, {3, 4}};

int[][] mat2 = {{5, 6}, {7, 8}};

int[][] result = new int[2][2];

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 2; j++) {

result[i][j] = mat1[i][j] + mat2[i][j];

[Link]("Matrix Addition Result:");

for (int[] row : result) {

for (int val : row) {

[Link](val + " ");

[Link]();

// Program 27: Check Armstrong Number

class Program27 {

public static void main(String[] args) {

int num = 153, sum = 0, temp = num;

while (temp != 0) {
int digit = temp % 10;

sum += [Link](digit, 3);

temp /= 10;

[Link](num + (num == sum ? " is an Armstrong Number" : " is not an Armstrong


Number"));

// Program 28: Find Second Largest Element in an Array

class Program28 {

public static void main(String[] args) {

int[] arr = {5, 1, 9, 3, 7};

int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;

for (int num : arr) {

if (num > largest) {

secondLargest = largest;

largest = num;

} else if (num > secondLargest && num != largest) {

secondLargest = num;

[Link]("Second Largest Element: " + secondLargest);

// Program 29: Calculate Sum of Digits in a Number

class Program29 {

public static void main(String[] args) {

int num = 12345, sum = 0;

while (num != 0) {
sum += num % 10;

num /= 10;

[Link]("Sum of Digits: " + sum);

// Program 30: Count Occurrence of Each Character in a String

class Program30 {

public static void main(String[] args) {

String str = "programming";

int[] freq = new int[256];

for (char c : [Link]()) {

freq[c]++;

for (int i = 0; i < [Link]; i++) {

if (freq[i] > 0) {

[Link]((char) i + ": " + freq[i]);

// Program 31: Simple Calculator

class Program31 {

public static void main(String[] args) {

int a = 10, b = 5;

[Link]("Addition: " + (a + b));

[Link]("Subtraction: " + (a - b));

[Link]("Multiplication: " + (a * b));

[Link]("Division: " + (a / b));


}

// Program 32: Implement Selection Sort

class Program32 {

public static void main(String[] args) {

int[] arr = {64, 25, 12, 22, 11};

for (int i = 0; i < [Link] - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < [Link]; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

// Program 33: Reverse a Sentence Word by Word

class Program33 {

public static void main(String[] args) {

String sentence = "Hello World Java";

String[] words = [Link](" ");

StringBuilder reversed = new StringBuilder();


for (int i = [Link] - 1; i >= 0; i--) {

[Link](words[i]).append(" ");

[Link]("Reversed Sentence: " + [Link]().trim());

// Program 34: Find Missing Number in an Array

class Program34 {

public static void main(String[] args) {

int[] arr = {1, 2, 4, 5, 6};

int n = 6, sum = 0;

for (int num : arr) {

sum += num;

int total = n * (n + 1) / 2;

[Link]("Missing Number: " + (total - sum));

// Program 35: Convert Binary to Decimal

class Program35 {

public static void main(String[] args) {

String binary = "1011";

int decimal = [Link](binary, 2);

[Link]("Decimal Equivalent: " + decimal);

// Program 36: Convert Decimal to Binary

class Program36 {
public static void main(String[] args) {

int decimal = 13;

String binary = [Link](decimal);

[Link]("Binary Equivalent: " + binary);

// (Continue Programs 37–100 similarly, incrementing complexity)

// Examples include:

// - File Handling (Reading/Writing Files)

// - Advanced OOP Concepts (Abstract Classes, Interfaces, Polymorphism)

// - Thread Management

// - Streams API and Lambda Expressions

// - Algorithms: Dijkstra, Kruskal, DFS/BFS

// - Sorting Techniques: Quick Sort, Merge Sort

// - Exception Handling Demonstrations

// - JavaFX GUI Basics

// Program 37: Check if a String is an Anagram of Another String

class Program37 {

public static void main(String[] args) {

String str1 = "listen", str2 = "silent";

char[] arr1 = [Link]();

char[] arr2 = [Link]();

[Link](arr1);

[Link](arr2);

[Link]("Anagram: " + [Link](arr1, arr2));

}
// Program 38: Count Number of Set Bits (1s) in a Binary Number

class Program38 {

public static void main(String[] args) {

int num = 29;

int count = 0;

while (num > 0) {

count += num & 1;

num >>= 1;

[Link]("Number of set bits: " + count);

// Program 39: Find Common Elements in Two Arrays

class Program39 {

public static void main(String[] args) {

int[] arr1 = {1, 2, 3, 4, 5};

int[] arr2 = {4, 5, 6, 7, 8};

[Link]("Common Elements:");

for (int num1 : arr1) {

for (int num2 : arr2) {

if (num1 == num2) {

[Link](num1 + " ");

// Program 40: Implement Quick Sort


class Program40 {

public static void main(String[] args) {

int[] arr = {10, 80, 30, 90, 40, 50, 70};

quickSort(arr, 0, [Link] - 1);

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

static void quickSort(int[] arr, int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

static int partition(int[] arr, int low, int high) {

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j < high; j++) {

if (arr[j] <= pivot) {

i++;

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return i + 1;
}

// Program 41: Implement Merge Sort

class Program41 {

public static void main(String[] args) {

int[] arr = {12, 11, 13, 5, 6, 7};

mergeSort(arr, 0, [Link] - 1);

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

static void mergeSort(int[] arr, int l, int r) {

if (l < r) {

int m = (l + r) / 2;

mergeSort(arr, l, m);

mergeSort(arr, m + 1, r);

merge(arr, l, m, r);

static void merge(int[] arr, int l, int m, int r) {

int n1 = m - l + 1;

int n2 = r - m;

int[] L = new int[n1];

int[] R = new int[n2];

[Link](arr, l, L, 0, n1);

[Link](arr, m + 1, R, 0, n2);

int i = 0, j = 0;

int k = l;

while (i < n1 && j < n2) {


if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

while (j < n2) {

arr[k] = R[j];

j++;

k++;

// Program 42: Implement Binary Search (Recursive)

class Program42 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};

int target = 5;

int result = binarySearch(arr, target, 0, [Link] - 1);

[Link]("Element found at index: " + result);

static int binarySearch(int[] arr, int target, int low, int high) {
if (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

return binarySearch(arr, target, mid + 1, high);

} else {

return binarySearch(arr, target, low, mid - 1);

return -1;

// Program 43: Print Pascal's Triangle

class Program43 {

public static void main(String[] args) {

int rows = 5;

for (int i = 0; i < rows; i++) {

int num = 1;

for (int j = 0; j <= i; j++) {

[Link](num + " ");

num = num * (i - j) / (j + 1);

[Link]();

// Program 44: Convert Roman Numerals to Integer

class Program44 {
public static void main(String[] args) {

String roman = "IX";

int result = 0;

for (int i = 0; i < [Link](); i++) {

int current = romanToInt([Link](i));

int next = (i + 1 < [Link]()) ? romanToInt([Link](i + 1)) : 0;

if (current < next) {

result -= current;

} else {

result += current;

[Link]("Integer Equivalent: " + result);

static int romanToInt(char c) {

switch (c) {

case 'I': return 1;

case 'V': return 5;

case 'X': return 10;

case 'L': return 50;

case 'C': return 100;

case 'D': return 500;

case 'M': return 1000;

default: return 0;

// Program 45: Count Total Number of Words in a String

class Program45 {
public static void main(String[] args) {

String str = "Java is a powerful language";

String[] words = [Link](" ");

[Link]("Total Words: " + [Link]);

// Program 46: Remove Duplicates from Array

class Program46 {

public static void main(String[] args) {

int[] arr = {1, 2, 2, 3, 3, 4};

Set<Integer> uniqueSet = new HashSet<>();

for (int num : arr) {

[Link](num);

[Link]("Array without duplicates: " + uniqueSet);

// Program 47: Find the First Non-Repeating Character in a String

class Program47 {

public static void main(String[] args) {

String str = "swiss";

for (char c : [Link]()) {

if ([Link](c) == [Link](c)) {

[Link]("First non-repeating character: " + c);

break;

}
// Program 48: Create a Simple Thread

class Program48 extends Thread {

public static void main(String[] args) {

Program48 t = new Program48();

[Link]();

public void run() {

[Link]("Thread is running");

// Program 49: Check if a Number is Prime Using a Method

class Program49 {

public static void main(String[] args) {

int num = 17;

[Link](num + " is " + (isPrime(num) ? "Prime" : "Not Prime"));

static boolean isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) return false;

return true;

// Program 50: Create a Singleton Class

class Program50 {
private static Program50 instance;

private Program50() {}

public static Program50 getInstance() {

if (instance == null) {

instance = new Program50();

return instance;

public static void main(String[] args) {

Program50 singleton = [Link]();

[Link]("Singleton Instance: " + singleton);

// Program 51: Reverse a String

class Program51 {

public static void main(String[] args) {

String str = "Hello World";

String reversed = new StringBuilder(str).reverse().toString();

[Link]("Reversed String: " + reversed);

// Program 52: Check if a Number is Even or Odd

class Program52 {
public static void main(String[] args) {

int num = 5;

[Link](num + " is " + (num % 2 == 0 ? "Even" : "Odd"));

// Program 53: Print Fibonacci Series up to n Numbers

class Program53 {

public static void main(String[] args) {

int n = 10;

int a = 0, b = 1;

[Link]("Fibonacci Series: " + a + " " + b);

for (int i = 2; i < n; i++) {

int next = a + b;

[Link](" " + next);

a = b;

b = next;

// Program 54: Count Number of Vowels in a String

class Program54 {

public static void main(String[] args) {

String str = "Programming";

int vowelCount = 0;

for (char c : [Link]().toCharArray()) {

if ("aeiou".indexOf(c) != -1) {

vowelCount++;

}
[Link]("Number of vowels: " + vowelCount);

// Program 55: Sum of Natural Numbers Using Recursion

class Program55 {

public static void main(String[] args) {

int n = 5;

[Link]("Sum of first " + n + " natural numbers: " + sumOfNumbers(n));

static int sumOfNumbers(int n) {

if (n == 0) return 0;

return n + sumOfNumbers(n - 1);

// Program 56: Find GCD (Greatest Common Divisor) of Two Numbers

class Program56 {

public static void main(String[] args) {

int a = 56, b = 98;

[Link]("GCD of " + a + " and " + b + ": " + gcd(a, b));

static int gcd(int a, int b) {

if (b == 0) return a;

return gcd(b, a % b);

// Program 57: Palindrome Check for a Number


class Program57 {

public static void main(String[] args) {

int num = 121;

int temp = num, reverse = 0;

while (temp != 0) {

int digit = temp % 10;

reverse = reverse * 10 + digit;

temp /= 10;

[Link](num + " is " + (num == reverse ? "Palindrome" : "Not Palindrome"));

// Program 58: Find Missing Element in an Array (Sum Formula Approach)

class Program58 {

public static void main(String[] args) {

int[] arr = {1, 2, 4, 5, 6};

int n = 6; // Assuming 1 to n

int totalSum = n * (n + 1) / 2;

int sumOfArray = 0;

for (int num : arr) {

sumOfArray += num;

[Link]("Missing Element: " + (totalSum - sumOfArray));

// Program 59: Reverse Words in a Sentence

class Program59 {

public static void main(String[] args) {

String sentence = "Java is awesome";


String[] words = [Link](" ");

StringBuilder reversedSentence = new StringBuilder();

for (int i = [Link] - 1; i >= 0; i--) {

[Link](words[i]).append(" ");

[Link]("Reversed Sentence: " + [Link]().trim());

// Program 60: Sum of Digits of a Number Using Recursion

class Program60 {

public static void main(String[] args) {

int num = 12345;

[Link]("Sum of Digits: " + sumOfDigits(num));

static int sumOfDigits(int num) {

if (num == 0) return 0;

return num % 10 + sumOfDigits(num / 10);

// Program 61: Count the Number of Words in a String

class Program61 {

public static void main(String[] args) {

String str = "Java programming is fun";

String[] words = [Link]("\\s+");

[Link]("Number of words: " + [Link]);

}
// Program 62: Count the Frequency of a Character in a String

class Program62 {

public static void main(String[] args) {

String str = "banana";

char ch = 'a';

int count = 0;

for (char c : [Link]()) {

if (c == ch) {

count++;

[Link]("Frequency of '" + ch + "': " + count);

// Program 63: Swap Two Numbers Without Using a Third Variable

class Program63 {

public static void main(String[] args) {

int a = 5, b = 10;

[Link]("Before Swap: a = " + a + ", b = " + b);

a = a + b;

b = a - b;

a = a - b;

[Link]("After Swap: a = " + a + ", b = " + b);

// Program 64: Find the Smallest Element in an Array

class Program64 {

public static void main(String[] args) {

int[] arr = {10, 20, 5, 8, 15};


int smallest = arr[0];

for (int num : arr) {

if (num < smallest) {

smallest = num;

[Link]("Smallest Element: " + smallest);

// Program 65: Calculate Factorial Using Iteration

class Program65 {

public static void main(String[] args) {

int num = 5;

int factorial = 1;

for (int i = 1; i <= num; i++) {

factorial *= i;

[Link]("Factorial of " + num + ": " + factorial);

// Program 66: Calculate Factorial Using Recursion

class Program66 {

public static void main(String[] args) {

int num = 5;

[Link]("Factorial of " + num + ": " + factorial(num));

static int factorial(int num) {

if (num == 0) return 1;
return num * factorial(num - 1);

// Program 67: Print Prime Numbers Between 1 and N

class Program67 {

public static void main(String[] args) {

int n = 50;

[Link]("Prime Numbers up to " + n + ":");

for (int i = 2; i <= n; i++) {

if (isPrime(i)) {

[Link](i + " ");

static boolean isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) return false;

return true;

// Program 68: Check if a String is a Valid Email

class Program68 {

public static void main(String[] args) {

String email = "test@[Link]";

[Link]("Valid Email: " + isValidEmail(email));

}
static boolean isValidEmail(String email) {

return [Link]("^[A-Za-z0-9+_.-]+@(.+)$");

// Program 69: Remove All White Spaces from a String

class Program69 {

public static void main(String[] args) {

String str = "Java programming is fun";

String result = [Link]("\\s", "");

[Link]("String without spaces: " + result);

// Program 70: Implement Bubble Sort

class Program70 {

public static void main(String[] args) {

int[] arr = {5, 3, 8, 1, 2};

bubbleSort(arr);

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

static void bubbleSort(int[] arr) {

for (int i = 0; i < [Link] - 1; i++) {

for (int j = 0; j < [Link] - 1 - i; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];


arr[j] = arr[j + 1];

arr[j + 1] = temp;

// Program 71: Reverse an Array

class Program71 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int start = 0, end = [Link] - 1;

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

[Link]("Reversed Array:");

for (int num : arr) {

[Link](num + " ");

// Program 72: Find Largest Number in an Array

class Program72 {

public static void main(String[] args) {

int[] arr = {10, 20, 4, 45, 99};


int max = arr[0];

for (int num : arr) {

if (num > max) {

max = num;

[Link]("Largest Number: " + max);

// Program 73: Check if a Number is a Perfect Number

class Program73 {

public static void main(String[] args) {

int num = 6;

int sum = 0;

for (int i = 1; i <= num / 2; i++) {

if (num % i == 0) {

sum += i;

[Link](num + " is " + (sum == num ? "Perfect" : "Not Perfect"));

// Program 74: Find the Second Largest Element in an Array

class Program74 {

public static void main(String[] args) {

int[] arr = {10, 20, 4, 45, 99};

int largest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;

for (int num : arr) {

if (num > largest) {


secondLargest = largest;

largest = num;

} else if (num > secondLargest && num != largest) {

secondLargest = num;

[Link]("Second Largest Element: " + secondLargest);

// Program 75: Find the Intersection of Two Arrays

class Program75 {

public static void main(String[] args) {

int[] arr1 = {1, 2, 3, 4, 5};

int[] arr2 = {4, 5, 6, 7, 8};

[Link]("Intersection: ");

for (int i : arr1) {

for (int j : arr2) {

if (i == j) {

[Link](i + " ");

// Program 76: Check if a String Contains Only Digits

class Program76 {

public static void main(String[] args) {

String str = "123456";

[Link](str + " contains only digits: " + [Link]("\\d+"));


}

// Program 77: Find the Sum of Even Numbers in an Array

class Program77 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5, 6};

int sum = 0;

for (int num : arr) {

if (num % 2 == 0) {

sum += num;

[Link]("Sum of Even Numbers: " + sum);

// Program 78: Count the Number of Words in a String Using Regular Expression

class Program78 {

public static void main(String[] args) {

String str = "Java programming is fun";

String[] words = [Link]().split("\\s+");

[Link]("Number of words: " + [Link]);

// Program 79: Find the Longest Word in a String

class Program79 {

public static void main(String[] args) {

String str = "Java programming is fun and challenging";

String[] words = [Link](" ");


String longestWord = "";

for (String word : words) {

if ([Link]() > [Link]()) {

longestWord = word;

[Link]("Longest Word: " + longestWord);

// Program 80: Check if Two Strings are Equal (Ignoring Case)

class Program80 {

public static void main(String[] args) {

String str1 = "hello";

String str2 = "HELLO";

[Link]("Strings are equal ignoring case: " + [Link](str2));

// Program 81: Find the Sum of All Odd Numbers in an Array

class Program81 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5, 6};

int sum = 0;

for (int num : arr) {

if (num % 2 != 0) {

sum += num;

[Link]("Sum of Odd Numbers: " + sum);

}
}

// Program 82: Count the Number of Occurrences of a Character in a String

class Program82 {

public static void main(String[] args) {

String str = "programming";

char ch = 'g';

int count = 0;

for (char c : [Link]()) {

if (c == ch) {

count++;

[Link]("Occurrences of '" + ch + "': " + count);

// Program 83: Convert Binary to Decimal

class Program83 {

public static void main(String[] args) {

String binary = "1011";

int decimal = [Link](binary, 2);

[Link]("Decimal of " + binary + ": " + decimal);

// Program 84: Print a Right-Angle Triangle Pattern

class Program84 {

public static void main(String[] args) {

int n = 5;

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) {

[Link]("* ");

[Link]();

// Program 85: Implement Selection Sort

class Program85 {

public static void main(String[] args) {

int[] arr = {64, 25, 12, 22, 11};

selectionSort(arr);

[Link]("Sorted Array:");

for (int num : arr) {

[Link](num + " ");

static void selectionSort(int[] arr) {

for (int i = 0; i < [Link] - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < [Link]; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

int temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

}
}

// Program 86: Find the Maximum Subarray Sum (Kadane's Algorithm)

class Program86 {

public static void main(String[] args) {

int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

[Link]("Maximum Subarray Sum: " + maxSubArraySum(arr));

static int maxSubArraySum(int[] arr) {

int maxSum = arr[0], currentSum = arr[0];

for (int i = 1; i < [Link]; i++) {

currentSum = [Link](arr[i], currentSum + arr[i]);

maxSum = [Link](maxSum, currentSum);

return maxSum;

// Program 87: Check if a Number is a Strong Number

class Program87 {

public static void main(String[] args) {

int num = 145;

int sum = 0, temp = num;

while (temp > 0) {

int digit = temp % 10;

sum += factorial(digit);

temp /= 10;

[Link](num + " is " + (sum == num ? "Strong" : "Not Strong"));


}

static int factorial(int n) {

int fact = 1;

for (int i = 1; i <= n; i++) {

fact *= i;

return fact;

// Program 88: Find the Length of a String Without Using Built-in Functions

class Program88 {

public static void main(String[] args) {

String str = "Java Programming";

int length = 0;

for (char c : [Link]()) {

length++;

[Link]("Length of the string: " + length);

// Program 89: Sum of First n Prime Numbers

class Program89 {

public static void main(String[] args) {

int n = 5;

int sum = 0;

int count = 0;

int num = 2;
while (count < n) {

if (isPrime(num)) {

sum += num;

count++;

num++;

[Link]("Sum of first " + n + " prime numbers: " + sum);

static boolean isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) return false;

return true;

// Program 90: Merge Two Sorted Arrays

class Program90 {

public static void main(String[] args) {

int[] arr1 = {1, 3, 5, 7};

int[] arr2 = {2, 4, 6, 8};

int[] result = new int[[Link] + [Link]];

int i = 0, j = 0, k = 0;

while (i < [Link] && j < [Link]) {

if (arr1[i] < arr2[j]) {

result[k++] = arr1[i++];

} else {
result[k++] = arr2[j++];

while (i < [Link]) {

result[k++] = arr1[i++];

while (j < [Link]) {

result[k++] = arr2[j++];

[Link]("Merged Array: ");

for (int num : result) {

[Link](num + " ");

// Program 91: Count the Number of Prime Numbers Between Two Numbers

class Program91 {

public static void main(String[] args) {

int start = 10, end = 50;

int count = 0;

for (int i = start; i <= end; i++) {

if (isPrime(i)) {

count++;

[Link]("Number of prime numbers between " + start + " and " + end + ": " + count);

}
static boolean isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i <= [Link](num); i++) {

if (num % i == 0) return false;

return true;

// Program 92: Find the Missing Number in an Array

class Program92 {

public static void main(String[] args) {

int[] arr = {1, 2, 4, 5, 6};

int n = 6; // Expected range is 1 to 6

int totalSum = n * (n + 1) / 2;

int sumOfArray = 0;

for (int num : arr) {

sumOfArray += num;

[Link]("Missing Number: " + (totalSum - sumOfArray));

// Program 93: Convert Decimal to Binary

class Program93 {

public static void main(String[] args) {

int decimal = 10;

String binary = [Link](decimal);

[Link]("Decimal: " + decimal + " -> Binary: " + binary);

}
}

// Program 94: Find the Majority Element in an Array (if exists)

class Program94 {

public static void main(String[] args) {

int[] arr = {3, 3, 4, 2, 4, 4, 2, 4, 4};

int majorityElement = findMajorityElement(arr);

[Link]("Majority Element: " + majorityElement);

static int findMajorityElement(int[] arr) {

int count = 0, majorityElement = -1;

for (int num : arr) {

if (count == 0) {

majorityElement = num;

count += (num == majorityElement) ? 1 : -1;

// Check if the element is majority

count = 0;

for (int num : arr) {

if (num == majorityElement) count++;

return count > [Link] / 2 ? majorityElement : -1;

// Program 95: Print the Factorial of a Number Using Lambda Expression

class Program95 {

public static void main(String[] args) {


int num = 5;

Factorial factorial = (n) -> {

int fact = 1;

for (int i = 1; i <= n; i++) {

fact *= i;

return fact;

};

[Link]("Factorial of " + num + ": " + [Link](num));

interface Factorial {

int calculate(int n);

// Program 96: Find the Middle Element in an Array

class Program96 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int middleElement = arr[[Link] / 2];

[Link]("Middle Element: " + middleElement);

// Program 97: Print the Fibonacci Series Using Recursion

class Program97 {

public static void main(String[] args) {

int n = 10;

[Link]("Fibonacci Series: ");

for (int i = 0; i < n; i++) {


[Link](fibonacci(i) + " ");

static int fibonacci(int n) {

if (n <= 1) return n;

return fibonacci(n - 1) + fibonacci(n - 2);

// Program 98: Find All Subsets of a Set

class Program98 {

public static void main(String[] args) {

int[] set = {1, 2, 3};

[Link]("Subsets of the set: ");

findSubsets(set);

static void findSubsets(int[] set) {

int n = [Link];

for (int i = 0; i < (1 << n); i++) {

[Link]("{ ");

for (int j = 0; j < n; j++) {

if ((i & (1 << j)) > 0) {

[Link](set[j] + " ");

[Link]("}");

}
// Program 99: Count the Number of Set Bits (1s) in a Number

class Program99 {

public static void main(String[] args) {

int num = 15; // Binary: 1111

int count = 0;

while (num > 0) {

count += num & 1;

num >>= 1;

[Link]("Number of set bits: " + count);

// Program 100: Rotate an Array to the Right by k Positions

class Program100 {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

int k = 2; // Rotate by 2 positions

rotateArray(arr, k);

[Link]("Rotated Array: ");

for (int num : arr) {

[Link](num + " ");

static void rotateArray(int[] arr, int k) {

int n = [Link];

k = k % n;

reverse(arr, 0, n - 1);

reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);

static void reverse(int[] arr, int start, int end) {

while (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

start++;

end--;

// Program 101: Simple Calculator using Swing

import [Link].*;

import [Link].*;

class Program101 {

public static void main(String[] args) {

JFrame frame = new JFrame("Calculator");

JTextField tf1 = new JTextField();

JTextField tf2 = new JTextField();

JTextField result = new JTextField();

[Link](false);

JButton add = new JButton("+");

JButton subtract = new JButton("-");


[Link](50, 50, 150, 20);

[Link](50, 100, 150, 20);

[Link](50, 150, 50, 30);

[Link](120, 150, 50, 30);

[Link](50, 200, 150, 20);

[Link](e -> {

int num1 = [Link]([Link]());

int num2 = [Link]([Link]());

[Link]([Link](num1 + num2));

});

[Link](e -> {

int num1 = [Link]([Link]());

int num2 = [Link]([Link]());

[Link]([Link](num1 - num2));

});

[Link](tf1);

[Link](tf2);

[Link](result);

[Link](add);

[Link](subtract);

[Link](300, 400);

[Link](null);

[Link](true);

// Program 102: Login Form with Swing


import [Link].*;

import [Link].*;

class Program102 {

public static void main(String[] args) {

JFrame frame = new JFrame("Login Form");

JLabel l1 = new JLabel("Username:");

JLabel l2 = new JLabel("Password:");

JTextField tf = new JTextField();

JPasswordField pf = new JPasswordField();

JButton login = new JButton("Login");

[Link](50, 50, 100, 20);

[Link](150, 50, 150, 20);

[Link](50, 100, 100, 20);

[Link](150, 100, 150, 20);

[Link](100, 150, 100, 30);

[Link](e -> {

String username = [Link]();

String password = new String([Link]());

if ([Link]("admin") && [Link]("password")) {

[Link](frame, "Login Successful");

} else {

[Link](frame, "Invalid Credentials");

});

[Link](l1);

[Link](tf);

[Link](l2);
[Link](pf);

[Link](login);

[Link](400, 300);

[Link](null);

[Link](true);

// Program 103: Traffic Light Simulation

import [Link].*;

import [Link].*;

import [Link].*;

class Program103 extends JFrame implements ActionListener {

JRadioButton red, yellow, green;

JPanel panel;

Program103() {

red = new JRadioButton("Red");

yellow = new JRadioButton("Yellow");

green = new JRadioButton("Green");

ButtonGroup group = new ButtonGroup();

[Link](red);

[Link](yellow);

[Link](green);

panel = new JPanel() {

public void paintComponent(Graphics g) {

[Link](g);

[Link]([Link]);
[Link](50, 50, 50, 50);

[Link](50, 110, 50, 50);

[Link](50, 170, 50, 50);

if ([Link]()) [Link]([Link]);

else if ([Link]()) [Link]([Link]);

else if ([Link]()) [Link]([Link]);

if ([Link]()) [Link](50, 50, 50, 50);

if ([Link]()) [Link](50, 110, 50, 50);

if ([Link]()) [Link](50, 170, 50, 50);

};

[Link](this);

[Link](this);

[Link](this);

add(red, [Link]);

add(yellow, [Link]);

add(green, [Link]);

add(panel, [Link]);

setSize(200, 400);

setVisible(true);

public void actionPerformed(ActionEvent e) {

[Link]();

}
public static void main(String[] args) {

new Program103();

// Program 104: Applet that Displays "Hello, World!"

import [Link].*;

import [Link].*;

public class Program104 extends Applet {

public void paint(Graphics g) {

[Link]("Hello, World!", 50, 50);

// Program 105: Draw Shapes in an Applet

import [Link].*;

import [Link].*;

public class Program105 extends Applet {

public void paint(Graphics g) {

[Link]([Link]);

[Link](50, 50, 100, 50);

[Link]([Link]);

[Link](200, 50, 100, 50);

}
// Program 106: Multithreading Example

class Program106 extends Thread {

public void run() {

for (int i = 1; i <= 5; i++) {

[Link]([Link]().getName() + ": " + i);

try {

[Link](1000);

} catch (InterruptedException e) {

[Link]([Link]());

public static void main(String[] args) {

Program106 t1 = new Program106();

Program106 t2 = new Program106();

[Link]();

[Link]();

// Program 107: Synchronization Example

class Counter {

int count = 0;

synchronized void increment() {

count++;

}
class Program107 extends Thread {

Counter counter;

Program107(Counter counter) {

[Link] = counter;

public void run() {

for (int i = 0; i < 1000; i++) {

[Link]();

public static void main(String[] args) throws InterruptedException {

Counter counter = new Counter();

Program107 t1 = new Program107(counter);

Program107 t2 = new Program107(counter);

[Link]();

[Link]();

[Link]();

[Link]();

[Link]("Final Count: " + [Link]);

// Program 108: Simple HTTP Server


import [Link];

import [Link];

import [Link];

import [Link];

public class Program108 {

public static void main(String[] args) throws IOException {

HttpServer server = [Link](new InetSocketAddress(8080), 0);

[Link]("/", exchange -> {

String response = "Hello, World!";

[Link](200, [Link]().length);

OutputStream os = [Link]();

[Link]([Link]());

[Link]();

});

[Link]();

[Link]("Server started at [Link]

// Program 109: Servlet Example

import [Link].*;

import [Link].*;

import [Link].*;

public class Program109 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws


IOException {

[Link]("text/html");

PrintWriter out = [Link]();

[Link]("<h1>Hello, Servlet World!</h1>");


}

You might also like