Top 10 Most Asked Java Programs for Beginners in Interviews with Output 2026

Author: Ritika
0

 

If you are a B.Tech student like me, then you already know how confusing coding feels in the beginning. I still remember my first Java lab class… I didn’t even know where to start 😅. Syntax errors, semicolons, and those curly braces {} felt like enemies.

But honestly, things started making sense when I began practicing small programs. Not big projects, not complex logic… just simple Java programs.

In simple words, these beginner programs are like your “gym warm-up” before heavy workouts. They build your basics.

In this blog, I’ll explain Top 10 Java Programs for Beginners with Output, in a very simple way — like I would explain to my juniors before exams or lab viva.

  • Basic logic building
  • Input/output handling
  • Conditions & loops
  • Small real-life examples

So let’s start 🚀

(toc) #title=(Table of Content)

 

Program 1: Hello World Program

 

Explanation

This is the first program every student writes. Literally every student 😄

 

In this program we try to print Hello World! and understand how System.out.println works in Java.

 

First, we create a class named HelloWorld. Inside it we write the main method, which takes a string array as an argument. The main method is the entry point of every Java program. Then, inside main, we use System.out.println to print the message “Hello, World!” on the screen.

 

Code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

 

Output:

Hello, World!

 

Concept

  • public class → Class name
  • main() → Entry point of program
  • System.out.println() → Used to print output

In simple words:
This program just prints text on screen.

 

Program 2: Add Two Numbers

 

Explanation

  • This is one of the most common questions in exams and viva.
  • We declare two integer variables a and b with values 10 and 20.
  • We calculate their sum using the + operator and store it in another variable sum .
  • Finally, we print the result using System.out.println .
  • This program helps us understand how variables are declared, initialized, and used in arithmetic operations.

 

Code:

public class AddNumbers {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int sum = a + b;

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

 

Output:

Sum = 30

 

Concept

  • Variables (int a, b)
  • Arithmetic operator +

Real-life example:
Adding marks of two subjects 😄

 

Program 3: Take Input from User

 

Explanation

  • Many students get confused at first because Java needs a special class called Scanner to take input from the user.
  • We import java.util.Scanner at the top of the program to use it.
  • Inside main, we create an object sc of the Scanner class to read input from the keyboard.
  • We ask the user to enter a number using System.out.print.
  • The entered value is stored in variable num using sc.nextInt().
  • Finally, we display the entered number using System.out.println.

 

Code:

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number: ");
        int num = sc.nextInt();

        System.out.println("You entered: " + num);
    }
}

 

Output:

Enter number: 5
You entered: 5

 

Concept

  • Scanner class
  • nextInt() method

Tip: Always remember to import Scanner.

 

Program 4: Even or Odd Number

 

Explanation

  • This is a very important logic-based question often asked in exams and interviews.
  • We declare an integer variable num and assign it the value 7.
  • We use the modulus operator % to check the remainder when num is divided by 2.
  • If the remainder is 0 (num % 2 == 0), then the number is Even.
  • Otherwise, the number is Odd.
  • We print the result using System.out.println.

 

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

 

Concept

  • if-else condition
  • % (modulus operator)

In simple words:
If number is divisible by 2 → Even
Otherwise → Odd

 

Program 5: Find Largest of Three Numbers

 

Explanation

  • This is a commonly asked program in lab exams to test logical thinking.
  • We declare three integer variables a, b, and c with values 10, 25, and 15.
  • We use conditional statements (if, else if, else) to compare the values.
  • If a is greater than or equal to both b and c, then a is the largest.
  • If not, we check whether b is greater than or equal to both a and c. If true, then b is the largest.
  • Otherwise, c is the largest number.
  • Finally, we print the largest value using System.out.println.

 

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 is: " + a);
        } else if(b >= a && b >= c) {
            System.out.println("Largest is: " + b);
        } else {
            System.out.println("Largest is: " + c);
        }
    }
}

 

Output:

Largest is: 25

 

Concept

  • Logical operators (&&)
  • Multiple conditions

Real-life example:
Comparing marks of 3 students.

 

Program 6: Print Table of a Number

 

Explanation

  • This program prints the multiplication table of a number.
  • We declare an integer variable num and assign it the value 5.
  • We use a for loop that runs from i = 1 to i = 10.
  • In each iteration, we multiply num with i and print the result using System.out.println.
  • This way, the program displays the multiplication table of 5 from 1 to 10.

I remember practicing this before my 1st semester exam 😄

 

Code:

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

        for(int i = 1; i <= 10; i++) {
            System.out.println(num * i);
        }
    }
}

 

Output:


5
10
15
20
...
50

 

Concept

  • for loop
  • Iteration

 

In simple words:
Loop repeats task multiple times.

 

Program 7: Factorial of a Number

 

Explanation

  • Factorial is a very important concept for logic building and is often asked in exams.
  • We declare an integer variable num and assign it the value 5.
  • We also declare another variable fact and initialize it with 1 (because factorial starts with multiplication identity).
  • We use a for loop that runs from i = 1 to i = num.
  • In each iteration, we multiply fact with i and store the result back into fact.
  • After the loop ends, fact contains the factorial of num.
  • Finally, we print the result using System.out.println.

 

Code:

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);
    }
}

 

Output:

Factorial = 120

 

Concept

  • Loop
  • Multiplication logic

 

Example:
5! = 5×4×3×2×1

 

Program 8: Reverse a Number

 

Explanation

  • This program reverses the digits of a number.
  • We declare an integer variable num and assign it the value 123.
  • We also declare another variable reverse and initialize it with 0 (to store the reversed number).
  • We use a while loop that runs until num becomes 0.
  • Inside the loop, we extract the last digit using num % 10 and store it in digit.
  • We update reverse by multiplying it by 10 and adding digit.
  • We remove the last digit from num using num / 10.
  • After the loop ends, reverse contains the reversed number.
  • Finally, we print the result using System.out.println.

This one looks tricky but is actually simple.

 

Code:

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

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

        System.out.println("Reversed: " + reverse);
    }
}

 

Output:

Reversed: 321

 

Concept

  • while loop
  • Digit extraction

When I learned this:
It felt confusing first, but after dry run it became easy.

 

Program 9: Check Prime Number

 

Explanation

  • Prime numbers are very important in interviews and competitive exams.
  • We declare an integer variable num and assign it the value 7.
  • We also declare a boolean variable isPrime and set it to true initially.
  • We use a for loop starting from i = 2 up to i < num.
  • In each iteration, we check if num % i == 0. If true, it means num is divisible by i and hence not prime.
  • If divisibility is found, we set isPrime to false and break the loop.
  • After the loop ends, if isPrime is still true, then num is prime.
  • Finally, we print the result using System.out.println.

 

Code:


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

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

        if(isPrime) {
            System.out.println("Prime Number");
        } else {
            System.out.println("Not Prime");
        }
    }
}

 

Output:

Prime Number

 

Concept

  • Loop
  • Flag variable

Tip:
Many students forget break statement.

 

Program 10: Fibonacci Series

 

Explanation

  • Fibonacci series is a very popular question in exams and interviews.
  • We declare two integer variables a and b with initial values 0 and 1.
  • We first print a and b as the starting numbers of the series.
  • We use a for loop starting from i = 2 up to i < 10 to generate the next terms.
  • Inside the loop, we calculate the next number c as a + b.
  • We print c and then update a and b for the next iteration.
  • This process continues until the loop ends, giving us the first 10 Fibonacci numbers.

 

Code:


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

        System.out.print(a + " " + b);

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

 

Output:


0 1 1 2 3 5 8 13 21 34

 

Concept

  • Sequence generation
  • Loop + variables

 

Key Concepts Covered

ConceptDescription
VariablesStore data values
Data Typesint, float, etc.
Operators+, -, %, etc.
Conditionsif-else
Loopsfor, while
InputScanner
Logic BuildingStep-by-step thinking

 

Important Points

  • Practice is everything
  • Start with small programs
  • Don’t fear errors (they are normal)
  • Always dry run your code

 

Conclusion

So yeah, these were the Top 10 Java Programs for Beginners.

If you are in 1st or 2nd year B.Tech, trust me — these programs are enough to build your base strong.

When I started, I used to just copy code. But later I realized, understanding logic is more important than memorizing.

My personal advice:

  • Write code by yourself
  • Try changing values
  • Practice daily (even 30 minutes is enough)

Once you are comfortable with these, you can move to:

  • Arrays
  • Strings
  • OOP concepts

And slowly, you’ll become confident in Java 💪

 

FAQs

1. Which Java program should I start with?

Start with Hello World, then move to basic programs like addition and loops.

2. Is Java difficult for beginners?

Not really. In the beginning it feels tough, but with practice it becomes easy.

3. How many programs should I practice daily?

At least 2–3 programs daily is enough.

4. Why are loops important in Java?

Loops help in repeating tasks and are used in almost every program.

5. How to improve coding skills in Java?

Practice regularly

Solve problems

Work on small projects

 

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !)#days=(20)

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. By clicking "Accept", you consent to our use of cookies. Privacy Policy Cookies Policy
Accept !
Breaking News | News |