Top 10 Java Program for freshers



1. Hello World: 

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

The provided Java program is a classic "Hello, World!" example. It introduces the basic structure of a Java program:

  • It defines a class named HelloWorld.
  • The class contains a main method, which is the starting point of the program.
  • The main method uses System.out.println("Hello, World!"); to print the message "Hello, World!" to the console.
2. Factorial Calculation:

public class Factorial {
    public static int calculateFactorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * calculateFactorial(n - 1);
    }

    public static void main(String[] args) {
        int number = 5; // You can change this to any positive integer
        System.out.println("Factorial of " + number + " is: " + calculateFactorial(number));
    }
}

3. Palindrome Check:

public class Palindrome {
    public static boolean isPalindrome(String str) {
        String reversed = new StringBuilder(str).reverse().toString();
        return str.equals(reversed);
    }

    public static void main(String[] args) {
        String word = "radar"; // You can change this to any string
        System.out.println("Is \"" + word + "\" a palindrome? " + isPalindrome(word));
    }
}


4. Fibonacci Series:

public class Fibonacci {
    public static void printFibonacci(int n) {
        int a = 0, b = 1;
        System.out.print(a + " " + b + " ");
        for (int i = 2; i < n; i++) {
            int c = a + b;
            System.out.print(c + " ");
            a = b;
            b = c;
        }
    }

    public static void main(String[] args) {
        int count = 5; // You can change this to any positive integer
        System.out.println("Fibonacci Series up to " + count + " terms: ");
        printFibonacci(count);
    }
}

5. Reverse a String:

public class ReverseString {
    public static String reverse(String str) {
        return new StringBuilder(str).reverse().toString();
    }

    public static void main(String[] args) {
        String original = "hello"; // You can change this to any string
        System.out.println("Reversed String: " + reverse(original));
    }
}

6. Array Rotation:

public class ArrayRotation {
    public static void rotateArray(int[] arr, int k) {
        int n = arr.length;
        k = k % n;
        for (int i = 0; i < n; i++) {
            System.out.print(arr[(i + k) % n] + " ");
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5}; // You can change this to any array
        int rotationCount = 2; // You can change this to any positive integer
        System.out.println("Array after rotation: ");
        rotateArray(array, rotationCount);
    }
}

7. Armstrong Number:

public class ArmstrongNumber {
    public static boolean isArmstrong(int num) {
        int original = num;
        int result = 0;
        while (num != 0) {
            int remainder = num % 10;
            result += Math.pow(remainder, 3);
            num /= 10;
        }
        return result == original;
    }

    public static void main(String[] args) {
        int number = 153; // You can change this to any positive integer
        System.out.println(number + " is an Armstrong number? " + isArmstrong(number));
    }
}

8. Prime Number Check:

public class PrimeNumber {
    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int number = 17; // You can change this to any positive integer
        System.out.println(number + " is a prime number? " + isPrime(number));
    }
}

9. Linked List Reversal:

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
    }
}

public class LinkedListReversal {
    public static Node reverseLinkedList(Node head) {
        Node prev = null;
        Node current = head;
        Node next = null;

        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        head = prev;
        return head;
    }

    // Example usage:
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        // Add more nodes as needed

        System.out.println("Original Linked List: ");
        // Print original linked list

        head = reverseLinkedList(head);

        System.out.println("\nReversed Linked List: ");
        // Print reversed linked list
    }
}

10. Binary Search:

public class BinarySearch {
    public static int binarySearch(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }

        return -1; // Target not found
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // You can change this to any sorted array
        int target = 7; // You can change this to any integer
        int result = binarySearch(array, target);
        System.out.println("Index of " + target + " in the array: " + result);
    }
}

Comments

Popular posts from this blog

Emertxe Specialized Training in Embedded Systems and IoT for Career Preparedness

Accenture Virtual Internship Certificate