Essential Java Algorithms and Programming Fundamentals
Bubble Sort Algorithm in Java
Bubble Sort is a fundamental sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It is known for its simplicity, though it is inefficient for large lists.
import java.util.Scanner;
class program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
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();
}
// The sorting logic
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println("Sorted array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}Linear Search Implementation in Java
Linear Search (or Sequential Search) is the simplest searching algorithm. It checks every element in the list sequentially until the desired element (the key) is found or the end of the list is reached.
import java.util.Scanner;
class program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of 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("Enter the element to search:");
int key = sc.nextInt();
boolean found = false;
// The searching logic
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
System.out.println("Element " + key + " found at position " + (i + 1));
found = true;
break;
}
}
if (found == false) {
System.out.println("Element " + key + " not found in the array.");
}
}
}Generating the Fibonacci Sequence in Java
This program calculates the nth term of the Fibonacci sequence, where each number is the sum of the two preceding ones, starting from 0 and 1.
import java.util.Scanner;
class fibonaaci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the position(n): ");
int n = sc.nextInt();
int a = 0, b = 1, fib = 0;
// The Fibonacci logic
if (n == 1) {
fib = a;
} else if (n == 2) {
fib = b;
} else {
for (int i = 3; i <= n; i++) {
fib = a + b;
a = b;
b = fib;
}
}
System.out.println("The " + n + "th Fibonacci number is: " + fib);
}
}Java Program to Check for a Prime Number
This program determines if a given integer (passed as a command-line argument) is a prime number by counting its divisors. A prime number has exactly two divisors: 1 and itself.
class program {
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
int cnt = 0;
// The prime check logic
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
cnt++;
}
}
if (cnt == 2) {
System.out.println("It is a Prime Number!!");
} else {
System.out.println("It is not a Prime Number!!");
}
}
}Calculating Factorial in Java
A program to compute the factorial of a non-negative integer ($n!$), handling edge cases for 0 and negative inputs. Note that the input is expected via command-line arguments.
class factorial {
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
long factorial = 1;
// The factorial logic
if (num < 0) {
System.out.println("Factorial is not defined for negative numbers!!");
} else if (num == 0) {
System.out.println("Factorial is 1!!");
} else {
for (int i = 1; i <= num; i++) {
factorial *= i;
}
System.out.println("Factorial of " + num + " is: " + factorial);
}
}
}Sum of Series (i/i!) Calculation
This Java program calculates the sum of the series $\sum_{i=1}^{n} \frac{i}{i!}$ up to a specified limit n, utilizing a helper method for factorial calculation.
import java.util.Scanner;
class sumdiv {
public static long factorial(int num) {
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit:");
int num = sc.nextInt();
double sum = 0.0;
// The series sum logic
for (int i = 1; i <= num; i++) {
sum += (double) i / factorial(i);
}
System.out.println("Sum of the series up to " + num + " terms = " + sum);
}
}Calculating the Cyclic Sum of Digits
This program calculates the cyclic sum of digits for a given number represented as a string. This involves summing all partial sums derived from contiguous subsequences of digits.
import java.util.Scanner;
class cyclicsum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number:");
String num = sc.next();
int n = num.length();
int totalSum = 0;
// The cyclic sum logic
for (int i = 0; i < n; i++) {
int partialSum = 0;
for (int j = i; j < n; j++) {
partialSum += (num.charAt(j) - '0'); // Converts char to int
}
totalSum += partialSum;
}
System.out.println("Cyclic Sum = " + totalSum);
}
}PIN Generation Logic from Input String
This program generates a single-digit PIN by first calculating the total count of non-space characters (sum of word lengths) in the input string, and then finding the digital root of that total sum.
import java.util.Scanner;
public class pin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String:");
String input = sc.nextLine();
int sum = 0;
int count = 0;
// The PIN generation logic (calculating sum of word lengths)
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
if (ch != ' ') {
count++;
} else {
sum += count;
count = 0;
}
}
sum += count;
// Digital root calculation
while (sum >= 10) {
int temp = 0;
while (sum > 0) {
temp += sum % 10;
sum /= 10;
}
sum = temp;
}
System.out.println("Generated PIN=" + sum);
}
}