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.
- Check even or odd number
- Check positive or negative number
- Find largest of two numbers
- Find largest of three numbers
- Check prime number
- Print prime numbers between 1 to N
- Factorial of a number
- Fibonacci series
- Reverse a number
- Palindrome number
- Armstrong number
- Sum of digits
- Count digits in a number
- Swap two numbers using third variable
- Swap two numbers without third variable
- Find GCD of two numbers
- Find LCM of two numbers
- Check perfect number
- Check strong number
- Generate multiplication table
- Reverse a String
- Palindrome string
- Count vowels and consonants
- Count words in a string
- Remove white spaces
- Find duplicate characters
- Count character frequency
- Convert uppercase to lowercase
- Check anagram strings
- Remove duplicate characters
- Find first non-repeated character
- Find first repeated character
- Check string contains only digits
- Reverse words in a sentence
- Count occurrences of character
- Sum of array elements
- Largest element in array
- Smallest element in array
- Reverse array
- Sort array ascending
- Sort array descending
- Find second largest element
- Find second smallest element
- Remove duplicate elements from array
- Search element in array
- Merge two arrays
- Copy one array to another
- Find missing number in array
- Find duplicate number in array
- Rotate array left
- Rotate array right
- Matrix addition
- Matrix multiplication
- Transpose matrix
- Star triangle pattern
- Reverse star triangle pattern
- Pyramid pattern
- Number triangle pattern
- Floyd triangle
- Diamond pattern
- Class and object program
- Constructor program
- Inheritance program
- Method overloading program
- Method overriding program
- Encapsulation program
- Abstract class program
- Interface program
- Polymorphism program
- Static keyword program
- Final keyword program
- This keyword program
- Super keyword program
- Exception handling using try-catch
- Multiple catch block program
- Finally block program
- Throw keyword program
- Throws keyword program
- Custom exception program
- Read file using FileReader
- Write file using FileWriter
- Copy file program
- Serialization program
- Deserialization program
- ArrayList program
- LinkedList program
- HashSet program
- TreeSet program
- HashMap program
- TreeMap program
- Iterator program
- Collections sort program
- Thread using Thread class
- Thread using Runnable interface
- Synchronization program
- Lambda expression program
- Stream filter program
- Stream map program
- Optional class program
- 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.