10 sample programs covering different aspects of Java programming: - handbookforJava

Recent Posts

Tuesday, June 13, 2023

10 sample programs covering different aspects of Java programming:

 Here are 10 sample programs covering different aspects of Java programming:


Hello World:

java

_____________________________________________

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

Factorial Calculation:

java

_____________________________________________

public class Factorial {

    public static int factorial(int n) {

        if (n == 0)

            return 1;

        else

            return n * factorial(n - 1);

    }

    

    public static void main(String[] args) {

        int number = 5;

        int result = factorial(number);

        System.out.println("Factorial of " + number + " is " + result);

    }

}

Fibonacci Series:

java

_____________________________________________

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 = 10;

        printFibonacci(count);

    }

}

Sorting an Array:

java

_____________________________________________

import java.util.Arrays;


public class ArraySorting {

    public static void main(String[] args) {

        int[] numbers = {5, 2, 8, 1, 9};

        Arrays.sort(numbers);

        System.out.println(Arrays.toString(numbers));

    }

}

Reverse a String:

java

_____________________________________________

public class StringReversal {

    public static String reverseString(String str) {

        StringBuilder reversed = new StringBuilder();

        for (int i = str.length() - 1; i >= 0; i--) {

            reversed.append(str.charAt(i));

        }

        return reversed.toString();

    }

    

    public static void main(String[] args) {

        String word = "Hello";

        String reversedWord = reverseString(word);

        System.out.println("Reversed word: " + reversedWord);

    }

}

Checking Prime Numbers:

java

_____________________________________________

public class PrimeChecker {

    public static boolean isPrime(int number) {

        if (number <= 1)

            return false;

        

        for (int i = 2; i <= Math.sqrt(number); i++) {

            if (number % i == 0)

                return false;

        }

        

        return true;

    }

    

    public static void main(String[] args) {

        int num = 17;

        boolean isPrime = isPrime(num);

        System.out.println(num + " is prime? " + isPrime);

    }

}

Palindrome Check:

java

_____________________________________________

public class PalindromeChecker {

    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 = "madam";

        boolean isPalindrome = isPalindrome(word);

        System.out.println(word + " is a palindrome? " + isPalindrome);

    }

}

Factorial using Recursion:

java

_____________________________________________

public class Factorial {

    public static int factorial(int n) {

        if (n == 0)

            return 1;

        else

            return n * factorial(n - 1);

    }

    

    public static void main(String[] args) {

        int number = 5;

        int result = factorial(number);

        System.out.println("Factorial of " + number + " is " + result);

    }

}

Binary Search:

java

_____________________________________________

public class BinarySearch {

    public static int binarySearch(int[] arr, int target) {

        int left = 0;

        int right = arr.length - 1;

        

        while (left <= right) {

            int mid = left + (right - left) / 2;

            

            if (arr[mid] == target)

                return mid;

            

            if (arr[mid] < target)

                left = mid + 1;

            else

                right = mid - 1;

        }

        

        return -1;

    }

    

    public static void main(String[] args) {

        int[] numbers = {2, 5, 8, 10, 12, 15};

        int target = 8;

        int index = binarySearch(numbers, target);

        System.out.println("Element found at index: " + index);

    }

}

Linear Search:

java

_____________________________________________

public class LinearSearch {

    public static int linearSearch(int[] arr, int target) {

        for (int i = 0; i < arr.length; i++) {

            if (arr[i] == target)

                return i;

        }

        

        return -1;

    }

    

    public static void main(String[] args) {

        int[] numbers = {4, 9, 2, 7, 1, 5};

        int target = 2;

        int index = linearSearch(numbers, target);

        System.out.println("Element found at index: " + index);

    }

}










No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here