Preparing for Java interviews requires more than just theoretical knowledge. Recruiters expect you to write clean, logical, and optimized programs on the spot. Whether you're a beginner or an experienced developer, practicing essential Java programs helps you build strong problem-solving skills and confidence.
In this comprehensive guide, you’ll explore 20 must-know Java programs with outputs that are frequently asked in interviews. These programs cover fundamental concepts such as loops, conditionals, arrays, strings, recursion, and object-oriented principles.
To practice these programs effectively, you can use an Online Java Compiler to write, test, and debug your code instantly without setting up a local environment.
Why Practice Java Programs for Interviews?
Before jumping into the programs, it’s important to understand why they matter:
- Strengthens logical thinking
- Improves coding speed and accuracy
- Helps in clearing technical rounds
- Builds a solid foundation in Java concepts
- Enhances debugging skills
1. Java Program to Print Hello World
Code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Explanation:
This is the simplest Java program used to understand the basic structure of a Java application.
2. Java Program to Add Two Numbers
Code:
public class AddNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 30
3. Java Program to Check Even or Odd
Code:
public class EvenOdd {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
Output:
Odd
4. Java Program to Find Largest of Three Numbers
Code:
public class LargestNumber {
public static void main(String[] args) {
int a = 10, b = 25, c = 15;
if (a >= b && a >= c)
System.out.println("Largest: " + a);
else if (b >= a && b >= c)
System.out.println("Largest: " + b);
else
System.out.println("Largest: " + c);
}
}
Output:
Largest: 25
5. Java Program to Check Leap Year
Code:
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println("Leap Year");
else
System.out.println("Not a Leap Year");
}
}
Output:
Leap Year
6. Java Program to Find Factorial of a Number
Code:
public class Factorial {
public static void main(String[] args) {
int num = 5;
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}
Output:
Factorial: 120
7. Java Program to Print Fibonacci Series
Code:
public class Fibonacci {
public static void main(String[] args) {
int n = 10, 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;
}
}
}
Output:
0 1 1 2 3 5 8 13 21 34
8. Java Program to Reverse a Number
Code:
public class ReverseNumber {
public static void main(String[] args) {
int num = 1234, reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed: " + reversed);
}
}
Output:
Reversed: 4321
9. Java Program to Check Palindrome Number
Code:
public class PalindromeNumber {
public static void main(String[] args) {
int num = 121, original = num, reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
if (original == reversed)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
Output:
Palindrome
10. Java Program to Check Prime Number
Code:
public class PrimeNumber {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}
Output:
Prime
11. Java Program to Print Multiplication Table
Code:
public class MultiplicationTable {
public static void main(String[] args) {
int num = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(num + " x " + i + " = " + (num * i));
}
}
}
Output:
5 x 1 = 5
...
5 x 10 = 50
12. Java Program to Count Digits in a Number
Code:
public class CountDigits {
public static void main(String[] args) {
int num = 12345, count = 0;
while (num != 0) {
num /= 10;
count++;
}
System.out.println("Digits: " + count);
}
}
Output:
Digits: 5
13. Java Program to Find Sum of Digits
Code:
public class SumOfDigits {
public static void main(String[] args) {
int num = 123, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 6
14. Java Program to Swap Two Numbers
Code:
public class SwapNumbers {
public static void main(String[] args) {
int a = 5, b = 10;
int temp = a;
a = b;
b = temp;
System.out.println("a = " + a + ", b = " + b);
}
}
Output:
a = 10, b = 5
15. Java Program to Check Armstrong Number
Code:
public class Armstrong {
public static void main(String[] args) {
int num = 153, sum = 0, temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if (sum == num)
System.out.println("Armstrong");
else
System.out.println("Not Armstrong");
}
}
Output:
Armstrong
16. Java Program to Print Star Pattern
Code:
public class StarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
17. Java Program to Find GCD
Code:
public class GCD {
public static void main(String[] args) {
int a = 36, b = 60;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
System.out.println("GCD: " + a);
}
}
Output:
GCD: 12
18. Java Program to Find LCM
Code:
public class LCM {
public static void main(String[] args) {
int a = 12, b = 15, lcm;
lcm = (a * b) / gcd(a, b);
System.out.println("LCM: " + lcm);
}
static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
Output:
LCM: 60
19. Java Program to Sort an Array
Code:
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
Output:
[1, 2, 5, 8]
20. Java Program to Find Duplicate Elements in Array
Code:
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1};
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.println("Duplicate: " + arr[j]);
}
}
}
}
}
Output:
Duplicate: 2
Duplicate: 1
Pro Tips to Crack Java Interviews
1. Focus on Logic, Not Just Syntax
Interviewers evaluate how you think, not just how you write code.
2. Practice Daily
Use an Online Java Compiler to regularly practice and test programs.
3. Learn Optimization
After solving a problem, try improving its time and space complexity.
4. Understand Concepts Deeply
Topics like OOPs, Collections, Exception Handling, and Multithreading are equally important.
5. Write Clean Code
- Use meaningful variable names
- Follow proper indentation
- Avoid unnecessary complexity
Conclusion
Mastering these 20 essential Java programs will significantly boost your confidence and performance in technical interviews. These problems are not just repetitive exercises—they form the foundation of advanced programming concepts.
Consistent practice using an Online Java Compiler will help you write efficient code, debug quickly, and prepare effectively for real-world coding challenges.
If you aim to excel in Java development, start with these programs, understand their logic deeply, and gradually move toward more complex problems.
Top comments (0)