Top 100 Java Programs For Beginners

In this post, we are going to see the Top 100 Java Programs For Beginners that are frequently asked in interview.

How to Find the Smallest Number and its Index From an Array?

If we have a given array int[] a = { 6, 4, 7, 8, 9, 1, 2, 3}; then the smallest element should be 1 and the index should be 5.

  1. Consider the first element(i.e. element at index is zero) is the smallest number.
  2. Iterate over the array and update the smallest number if the number at index i is smaller than smallest number (i.e. a[0])
  3. Capture the corresponding index number
package com.javatute;

public class FindSmallestNumberWithIndex {
	public static void main(String[] args) {
		int[] a = { 6, 4, 7, 8, 9, 1, 2, 3, };

		int smallestElement = a[0];
		int indexOfSmallestNumber = 0;
		for (int i = 1; i < a.length; i++) {
			if (a[i] < smallestElement) {
				smallestElement = a[i];
				indexOfSmallestNumber = i;
			}
		}
		System.out.println(
				"Smallest element is:- " + smallestElement + "  Index of smallestElementIs:- " + indexOfSmallestNumber);
	}
}

Output – Smallest element is:- 1 Index of smallestElmentIs:- 5

Note – To find largest Number and its Index From an Array we need to below code changes.

		int largestElement = a[0];
		int indexOfLargestElement = 0;
		for (int i = 1; i < a.length; i++) {
			if (a[i] > indexOfLargestElement) {
				largestElement = a[i];
				indexOfLargestElement = i;
			}
		}

How to find missing number from a sorted array?

Consider we have given an array {1, 2, 3, 4, 5, 6, 8, 9, 10}, since 7 is missing output should be 7.

  1. Find the sum of n number using the formula n(n+1)/2.
  2. Find the sum of the given numbers using the for loop.
  3. Subtract ssumOfGivenNumbers from sumOfNNumbers.
package com.javatute;

public class FindMissingNumber {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 8, 9, 10};
        int sumOfNNumbers = 10 * (10 + 1) / 2;

        int sumOfGivenNumbers = 0;
        for (int i = 0; i < arr.length; i++) {
            sumOfGivenNumbers = sumOfGivenNumbers + arr[i];
        }
        System.out.println(sumOfNNumbers - sumOfGivenNumbers);

    }
}

Output – 7

Trick – Sum of all numbers – Sum of the given numbers

Write a Program to find nth highest number in Array?

  1. Sort the array in Descending order.
  2. Compare the element with k -1 where k is the element that we want to find.
package com.javatute;

public class SecondLargestNumber {
    public static void main(String[] args) {
        int k = 2;
        int[] arr = {8, 2, 3, 5, 1, 7, 6, 4};
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] < arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            if (i == k - 1) {
                System.out.println(arr[i]);
            }
        }
    }
}

Output – 6

How to find the special characters from String?

Consider we have a String inputString= “hello#javatute@&” then output should be String newString = “#@&”

  1. Convert String to the character array
  2. Within the loop check for not alphabet, not number, and not whitespace.
package com.javatute;

public class FindSpecialCharacterFromString {
    public static void main(String[] args) {
        String inputString = "hello#javatute@&";
        char[] charArray = inputString.toCharArray();

        String newString = "";
        for (int i = 0; i < charArray.length; i++) {
            if (!Character.isAlphabetic(charArray[i]) && !Character.isDigit(charArray[i])
                    && !Character.isWhitespace(charArray[i])) {
                newString = newString + charArray[i];
            }
        }
        System.out.println(newString);
    }
}

Output – #@&

Write a Program to get the factorial of a number using recursion and without recursion.

If the number is 3 then the expected output is 6. If the number is 4 then the expected output is 24.

Using recursion

  1. Define a method that accepts int as a parameter and also returns a number.
  2. Decrease the number with one and get out from recursion if the number becomes zero.
package com.javatute;

public class FactorialUsingRecursion {
    static int getFactorial(int number){
        if (number == 0)
            return 1;
        else
            return(number * getFactorial(number-1));
    }
    public static void main(String[] args) {
        int factorial = getFactorial(4);
        System.out.println(factorial);
    }
}

Output – 24

Without using recursion

package com.javatute;

public class WithoutUsingRecursion {
    public static void main(String[] args) {
        int number = 5;
        int factorial = 1;
        while (number > 0) {
            factorial = factorial * number;
            number--;
        }
        System.out.println(factorial);
    }
}

Output – 120

How to find duplicate elements from Array?

Write a Program to check Armstrong Numbers in Java?

Find the nearest number which is equal to sum of elements of two array elements at same index.

For example consider we have two array as below.

Integer[] a = { 4, 10, 5, 6, 7, 8 };

Integer[] b = { 3, 1, 8, 4, 5, 31 };

A number is given 38. If we add elements of both array which is at same index 4+3, 10+1, 5+8, 6+4, 7+5, 8+31 we have numbers 7,11,13,10,12,39. The 39 is near to given number 38.

In this case expected output is 5 i.e. index of 39 because it is near to 38.

Same way if given number is 6 then expected output is 0 because 4+3 = 7 that is nearest to 6.

package com.javatute;

public class FindIndexOfNearestElementSumOfSameIndex {
	public static void main(String[] args) {
		Integer[] a = { 4, 10, 5, 6, 7, 8 };
		Integer[] b = { 3, 1, 8, 4, 5, 31 };
		int k = 38;

		int difference = 0;
		int sumOfNumbersAtSameIndex = 0;
		int[] finalArray = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			sumOfNumbersAtSameIndex = a[i] + b[i];
			if (sumOfNumbersAtSameIndex > k) {
				difference = sumOfNumbersAtSameIndex - k;
			} else {
				difference = k - sumOfNumbersAtSameIndex;
			}

			finalArray[i] = difference;
		}

		int indexOfSmallestNumber = 0;
		int smallestNumber = finalArray[0];
		for (int i = 1; i < finalArray.length; i++) {
			if (finalArray[i] < smallestNumber) {
				smallestNumber = finalArray[i];
				indexOfSmallestNumber = i;
			}
		}
		System.out.println("Elements is at index:- " + indexOfSmallestNumber);
	}
}

Output is:- Elements is at index:- 5

Write a program to check number is prime or not in Java.

Approach – Check for all numbers from 2 to that number, if the given number is divisible by any of these numbers.

package com.javatute;

public class PrimeNumberExample {
    public static void main(String[] args) {
        int n = 5;
        boolean isPrime = true;
        for (int i = 2; i < n - 1; i++) {
            if (n % i == 0) {
                isPrime = false;
            }
        }
        System.out.println(isPrime);
    }
}

Output is – true

Write a program to revere a string in Java.

public class Test3 {
    public static void main(String[] args) {
        String s1 = "mynameisrakesh";
        int length = s1.length();
        String rev = "";
        for (int i = length - 1; i >= 0; i--) {
            rev = rev + s1.charAt(i);
        }
        System.out.println(rev);
    }
}

Output is – hsekarsiemanym

Trick – Run the for loop in reverse order and use charAT() method.

Write a program to reverse of number in Java.

public class Test3 {
    public static void main(String[] args) {
        int number = 18976;
        int reverse = 0;
        while (number > 0) {
            int remainder = number % 10;
            reverse = reverse * 10 + remainder;
            number = number / 10;
        }
        System.out.println(reverse);
    }
}

Output – 67981

Trick – Define a new variable reverse and multiply with zero then add remainder. Update the number with the quotient.

That’s all about Top 100 Java Programs For Beginners.