Java Programs Top 100 with Explanation

This page contains the most important Top 100 Java programs for beginners, interviews, coding practice, and placement preparation. These programs cover numbers, strings, arrays, patterns, sorting, searching, recursion, OOPS, collections, and exception handling.

Number Programs in Java

Number programs are commonly asked in Java interviews. They help improve logic building, loop understanding, and mathematical problem-solving skills.

1. Check Even or Odd Number

A number is even if it is divisible by 2. Otherwise, it is odd.

public class EvenOdd {
    public static void main(String[] args) {
        int num = 10;

        if (num % 2 == 0) {
            System.out.println("Even Number");
        } else {
            System.out.println("Odd Number");
        }
    }
}

2. Check Positive or Negative Number

A number greater than zero is positive, less than zero is negative, and zero is neutral.

public class PositiveNegative {
    public static void main(String[] args) {
        int num = -5;

        if (num > 0) {
            System.out.println("Positive");
        } else if (num < 0) {
            System.out.println("Negative");
        } else {
            System.out.println("Zero");
        }
    }
}

3. Find Largest of Two Numbers

This program compares two numbers and prints the larger value.

public class LargestTwo {
    public static void main(String[] args) {
        int a = 10, b = 20;

        if (a > b) {
            System.out.println(a + " is largest");
        } else {
            System.out.println(b + " is largest");
        }
    }
}

4. Find Largest of Three Numbers

This program compares three values using conditional statements.

public class LargestThree {
    public static void main(String[] args) {
        int a = 10, b = 30, c = 20;

        if (a >= b && a >= c) {
            System.out.println(a + " is largest");
        } else if (b >= a && b >= c) {
            System.out.println(b + " is largest");
        } else {
            System.out.println(c + " is largest");
        }
    }
}

5. Check Prime Number

A prime number is divisible only by 1 and itself.

public class PrimeNumber {
    public static void main(String[] args) {
        int num = 17;
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }

        System.out.println(isPrime ? "Prime Number" : "Not Prime Number");
    }
}

6. Factorial of a Number

Factorial means multiplying a number by all positive numbers less than it.

public class Factorial {
    public static void main(String[] args) {
        int num = 5;
        int fact = 1;

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

        System.out.println("Factorial: " + fact);
    }
}

7. Fibonacci Series

Fibonacci series starts with 0 and 1. Each next number is the sum of previous two numbers.

public class Fibonacci {
    public static void main(String[] args) {
        int n = 10;
        int a = 0, b = 1;

        for (int i = 1; i <= n; i++) {
            System.out.print(a + " ");
            int c = a + b;
            a = b;
            b = c;
        }
    }
}

8. Reverse a Number

This program extracts digits from the end and builds the reverse number.

public class ReverseNumber {
    public static void main(String[] args) {
        int num = 12345;
        int rev = 0;

        while (num != 0) {
            int digit = num % 10;
            rev = rev * 10 + digit;
            num = num / 10;
        }

        System.out.println("Reverse: " + rev);
    }
}

9. Palindrome Number

A number is palindrome if it remains same after reversing.

public class PalindromeNumber {
    public static void main(String[] args) {
        int num = 121;
        int original = num;
        int rev = 0;

        while (num != 0) {
            int digit = num % 10;
            rev = rev * 10 + digit;
            num = num / 10;
        }

        if (original == rev) {
            System.out.println("Palindrome Number");
        } else {
            System.out.println("Not Palindrome");
        }
    }
}

10. Armstrong Number

An Armstrong number is a number whose sum of cubes of digits equals the original number.

public class ArmstrongNumber {
    public static void main(String[] args) {
        int num = 153;
        int original = num;
        int sum = 0;

        while (num != 0) {
            int digit = num % 10;
            sum = sum + digit * digit * digit;
            num = num / 10;
        }

        if (original == sum) {
            System.out.println("Armstrong Number");
        } else {
            System.out.println("Not Armstrong Number");
        }
    }
}

String Programs in Java

String programs are very important in Java interviews because they test logic, character handling, loops, and built-in string methods.

11. Reverse a String

This program reverses a String by reading characters from last index to first index.

public class ReverseString {
    public static void main(String[] args) {
        String str = "Java";
        String rev = "";

        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }

        System.out.println("Reverse: " + rev);
    }
}

12. Check Palindrome String

A String is palindrome if original and reversed strings are same.

public class PalindromeString {
    public static void main(String[] args) {
        String str = "madam";
        String rev = "";

        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }

        if (str.equals(rev)) {
            System.out.println("Palindrome String");
        } else {
            System.out.println("Not Palindrome");
        }
    }
}

13. Count Vowels and Consonants

This program checks every character and counts vowels and consonants separately.

public class CountVowels {
    public static void main(String[] args) {
        String str = "java programming";
        int vowels = 0, consonants = 0;

        str = str.toLowerCase();

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            if (ch >= 'a' && ch <= 'z') {
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                    vowels++;
                } else {
                    consonants++;
                }
            }
        }

        System.out.println("Vowels: " + vowels);
        System.out.println("Consonants: " + consonants);
    }
}

14. Count Words in a String

This program splits the string by spaces and counts total words.

public class CountWords {
    public static void main(String[] args) {
        String str = "Java is powerful";
        String[] words = str.split(" ");

        System.out.println("Words: " + words.length);
    }
}

15. Remove White Spaces

This program removes all spaces from a String using replaceAll().

public class RemoveSpaces {
    public static void main(String[] args) {
        String str = "J a v a";
        String result = str.replaceAll("\\s", "");

        System.out.println(result);
    }
}

Array Programs in Java

Array programs help understand indexing, loops, searching, sorting, and data manipulation.

16. Find Sum of Array Elements

This program adds all values present in an array.

public class ArraySum {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        int sum = 0;

        for (int num : arr) {
            sum = sum + num;
        }

        System.out.println("Sum: " + sum);
    }
}

17. Find Largest Element in Array

This program assumes first element is largest and compares it with remaining values.

public class LargestArray {
    public static void main(String[] args) {
        int[] arr = {10, 50, 20, 40};
        int max = arr[0];

        for (int num : arr) {
            if (num > max) {
                max = num;
            }
        }

        System.out.println("Largest: " + max);
    }
}

18. Find Smallest Element in Array

This program compares all array values and finds the smallest element.

public class SmallestArray {
    public static void main(String[] args) {
        int[] arr = {10, 5, 20, 40};
        int min = arr[0];

        for (int num : arr) {
            if (num < min) {
                min = num;
            }
        }

        System.out.println("Smallest: " + min);
    }
}

19. Reverse an Array

This program prints array elements from last index to first index.

public class ReverseArray {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40};

        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.print(arr[i] + " ");
        }
    }
}

20. Sort Array in Ascending Order

This program uses Arrays.sort() to sort elements.

import java.util.Arrays;

public class SortArray {
    public static void main(String[] args) {
        int[] arr = {30, 10, 50, 20};

        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
    }
}

Pattern Programs in Java

Pattern programs are useful for improving nested loop logic.

21. Star Triangle Pattern

This program prints stars in increasing order using nested loops.

public class StarPattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

22. Number Triangle Pattern

This program prints numbers in triangular format.

public class NumberPattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

OOPS Programs in Java

23. Class and Object Program

This program demonstrates how to create a class and object in Java.

class Student {
    int id;
    String name;

    void display() {
        System.out.println(id + " " + name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        s.id = 101;
        s.name = "Rahul";
        s.display();
    }
}

24. Inheritance Program

This program shows how one class can inherit another class.

class Animal {
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}

25. Method Overloading Program

Method overloading means same method name with different parameters.

class Calculator {
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
}

Java Top 100 Programs List

Below is a complete list of 100 Java programs for practice. You can create separate pages for each program later to improve SEO and user engagement.

  1. Check even or odd number
  2. Check positive or negative number
  3. Find largest of two numbers
  4. Find largest of three numbers
  5. Check prime number
  6. Print prime numbers between 1 to N
  7. Factorial of a number
  8. Fibonacci series
  9. Reverse a number
  10. Palindrome number
  11. Armstrong number
  12. Sum of digits
  13. Count digits in a number
  14. Swap two numbers using third variable
  15. Swap two numbers without third variable
  16. Find GCD of two numbers
  17. Find LCM of two numbers
  18. Check perfect number
  19. Check strong number
  20. Generate multiplication table
  21. Reverse a String
  22. Palindrome string
  23. Count vowels and consonants
  24. Count words in a string
  25. Remove white spaces
  26. Find duplicate characters
  27. Count character frequency
  28. Convert uppercase to lowercase
  29. Check anagram strings
  30. Remove duplicate characters
  31. Find first non-repeated character
  32. Find first repeated character
  33. Check string contains only digits
  34. Reverse words in a sentence
  35. Count occurrences of character
  36. Sum of array elements
  37. Largest element in array
  38. Smallest element in array
  39. Reverse array
  40. Sort array ascending
  41. Sort array descending
  42. Find second largest element
  43. Find second smallest element
  44. Remove duplicate elements from array
  45. Search element in array
  46. Merge two arrays
  47. Copy one array to another
  48. Find missing number in array
  49. Find duplicate number in array
  50. Rotate array left
  51. Rotate array right
  52. Matrix addition
  53. Matrix multiplication
  54. Transpose matrix
  55. Star triangle pattern
  56. Reverse star triangle pattern
  57. Pyramid pattern
  58. Number triangle pattern
  59. Floyd triangle
  60. Diamond pattern
  61. Class and object program
  62. Constructor program
  63. Inheritance program
  64. Method overloading program
  65. Method overriding program
  66. Encapsulation program
  67. Abstract class program
  68. Interface program
  69. Polymorphism program
  70. Static keyword program
  71. Final keyword program
  72. This keyword program
  73. Super keyword program
  74. Exception handling using try-catch
  75. Multiple catch block program
  76. Finally block program
  77. Throw keyword program
  78. Throws keyword program
  79. Custom exception program
  80. Read file using FileReader
  81. Write file using FileWriter
  82. Copy file program
  83. Serialization program
  84. Deserialization program
  85. ArrayList program
  86. LinkedList program
  87. HashSet program
  88. TreeSet program
  89. HashMap program
  90. TreeMap program
  91. Iterator program
  92. Collections sort program
  93. Thread using Thread class
  94. Thread using Runnable interface
  95. Synchronization program
  96. Lambda expression program
  97. Stream filter program
  98. Stream map program
  99. Optional class program
  100. LocalDate and LocalTime program

Summary

These Java Top 100 programs are useful for beginners, interview preparation, coding tests, and placement exams. Start with number and string programs, then move to arrays, patterns, OOPS, collections, file handling, and Java 8 programs.

Java Programs FAQ

Which Java programs are best for beginners?

Even or odd, prime number, factorial, Fibonacci, palindrome, reverse string, and array sum programs are best for beginners.

Are these Java programs useful for interviews?

Yes, these programs are commonly asked in fresher interviews, coding rounds, and Java placement tests.

How should I practice Java programs?

First understand the logic, then write the program without seeing the solution, and finally test with multiple inputs.