Java Interview Programs

Beginner
12 min read
Updated: Nov 2026
50 programs
Practice hub

Definition and Usage

Java interview programs are short practice problems used in Java tutorials, college labs, and fresher technical rounds. They reward clear loops, careful conditions, and clean Scanner / System.out I/O more than large application design.

What you practice

Most drills build the same Java skills: read input, process with for/while and if, print results, and handle edge cases. Number checks, digit math, arrays, matrices, and base conversions show up again and again.

What should beginners solve first?

Start with Even Number, Odd Number, and Prime Number. Then try Sum of Digits, Factorial, and Fibonacci Series before arrays and matrices.

This lesson vs program pages

This page is the Java interview hub: how to approach problems, which topic to practice next, and how each tutorial is structured. The programs index lists 50 individual guides—each with algorithm, complete code, output, and FAQs.

Narrate before you type.

In interviews, say the inputs, the loop bounds, and the expected output first. Then write the Java code.

Check edge cases.

Ask about 0, negatives, empty arrays, and overflow. Interviewers listen for that habit.

Compile tip.

Most labs build with javac FileName.java then java FileName. Prefer hasNextInt() before nextInt().

Java Syntax

A typical interview program reads a value, decides with a condition or loop, and prints the result:

Java
import java.util.Scanner;

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

        System.out.print("Enter a number: ");
        if (!sc.hasNextInt()) {
            System.out.println("Invalid input.");
            sc.close();
            return;
        }

        int n = sc.nextInt();
        if (n % 2 == 0) {
            System.out.println(n + " is even.");
        } else {
            System.out.println(n + " is odd.");
        }

        sc.close();
    }
}
/* javac InterviewSkeleton.java && java InterviewSkeleton */

Core Java Concepts

Skills these Java interview programs reinforce:

SkillWhat you practiceWhere it shows up
Language basicsTypes, operators, if/else, loops, methods, mainEvery number drill
Console I/OScanner, System.out.println, input checksInput/output in all labs
ArraysFixed arrays, index loops, .lengthSum / max / min of array
Collections (optional)ArrayList when size is unknownGrowing number lists
Digit mathn % 10, n / 10 loopsArmstrong, palindrome, digit sum
MatricesNested loops over 2D arraysAddition, multiply, transpose
RecursionBase cases, stack depth, divide-and-conquerFactorial, Fibonacci variants
Base conversionRemainders and place valuesDecimal ↔ binary / octal
Classes & helpersStatic methods when a problem grows past one blockGCD, factorial helpers

Learning order in this Java tutorial

StageFocus
1. Number basicsEven, odd, prime, perfect, palindrome
2. OperationsDigit sum, GCD/LCM, factorial, Fibonacci
3. ArraysSum, maximum, minimum, average
4. Matrix & conversion2D ops and number-base drills

Choose a Topic by Goal

Pick a practice cluster based on what you need to strengthen:

GoalStart here
Warm up conditionsEven Number, Odd Number
Loop + divisor logicPrime Number, Perfect Number
Digit extractionSum of Digits, Armstrong Number
Series / recursion styleFactorial, Fibonacci Series
Array traversalSum of Array, Maximum of Array
2D thinkingMatrix Addition, Matrix Transpose

Jump to a category

How to Practice Interview Programs in Java

A practical workflow for this tutorial track:

  1. Read the problem: inputs, outputs, and constraints (range, negatives, zero).
  2. Write the algorithm in plain language before coding.
  3. Implement with a public class, Scanner, clear names, and input checks.
  4. Test edge cases, then run: javac File.java && java File.
  5. Explain time complexity in one sentence (often O(1) or O(n) for these drills).
  6. Open the next program in the same category before jumping topics.

Live Preview

Enter an integer to preview the even/odd check—the same idea as Even Number and Odd Number.

Uses n % 2 == 0 for even, otherwise odd.

Preview
42 is even.

Interview tip: also mention 0 is even.

Java Examples

Three starter patterns from this Java tutorial track. Open the linked program page for the full walkthrough.

Example 1 — Even number check

The classic first interview warm-up in many Java labs.

Java
public class EvenCheck {
    public static void main(String[] args) {
        int n = 18;

        if (n % 2 == 0) {
            System.out.println(n + " is even.");
        } else {
            System.out.println(n + " is odd.");
        }
    }
}

How It Works

  1. % gives the remainder after division by 2.
  2. Remainder 0 means the number is even.
  3. System.out.println reports the result clearly for the interviewer.

Example 2 — Sum of digits

Digit extraction with n % 10 and n / 10.

Java
public class SumDigits {
    public static void main(String[] args) {
        int n = 482;
        int sum = 0;
        int temp = n;

        while (temp > 0) {
            sum += temp % 10;
            temp /= 10;
        }

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

How It Works

  1. temp % 10 peels off the last digit.
  2. temp /= 10 removes that digit.
  3. Loop until no digits remain.

Example 3 — Sum of an array

Fixed-array traversal—the bridge into matrix problems.

Java
public class SumArray {
    public static void main(String[] args) {
        int[] nums = { 4, 7, 1, 9 };
        int sum = 0;

        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }

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

How It Works

  1. length gives the element count.
  2. for visits each index once.
  3. sum accumulates the total—O(n) time.

Java Interview Programs Index

50 practice programs. Search by name or browse by category. Each row opens a full Java tutorial.

Number Basics

20 programs

Classify and verify special integers—classic warm-up questions in Java tutorials and campus rounds.

ProgramDescriptionTutorial
Even NumberCheck n % 2 == 0.Try it
Odd NumberCheck remainder 1.Try it
Natural NumberPositive integers.Try it
Composite NumberMore than two factors.Try it
Prime NumberOnly two factors.Try it
Perfect SquareSquare of an integer.Try it
Perfect NumberSum of proper divisors.Try it
Abundant NumberDivisor sum exceeds n.Try it
Strong NumberDigit factorial sum.Try it
Armstrong NumberPower sum of digits.Try it
Automorphic NumberSquare ends with n.Try it
Palindrome NumberReads the same reversed.Try it
Happy NumberDigit-square process.Try it
Harshad NumberDivisible by digit sum.Try it
Disarium NumberPositional power sum.Try it
Magic NumberRepeated digit sum is 1.Try it
Smith NumberDigit sum equals factor digits.Try it
Evil NumberEven count of 1-bits.Try it
Pronic NumberProduct of consecutives.Try it
Triangular Numbern(n+1)/2 form.Try it

Math and Operations

14 programs

Digit work, GCD/LCM, series, and everyday calculation drills.

ProgramDescriptionTutorial
Sum of DigitsAdd each digit of n.Try it
Condense a NumberReduce to a single digit.Try it
Swap Two NumbersExchange values safely.Try it
GCDGreatest common divisor.Try it
LCMLeast common multiple.Try it
Prime FactorFactorize into primes.Try it
Factorial of a Numbern! with a loop.Try it
Fibonacci SeriesGenerate the sequence.Try it
Leap YearCalendar leap rules.Try it
Power of 2Check 2^k form.Try it
Power of 3Check 3^k form.Try it
Cube NumberPerfect cubes.Try it
Number CombinationCombinations / nCr.Try it
Multiplication TablePrint a times table.Try it

Array Programs

6 programs

Traverse fixed arrays—sum, extremes, averages, and simple comparisons.

ProgramDescriptionTutorial
Sum of ArrayAdd all elements.Try it
Maximum Value of an ArrayFind the largest.Try it
Minimum Value of an ArrayFind the smallest.Try it
Average of N NumbersMean of N values.Try it
Biggest of Three NumbersCompare three ints.Try it
Common DivisorsShared divisors of two nums.Try it

Matrix and Conversion

10 programs

2D arrays, base conversions, and a few structured number problems.

ProgramDescriptionTutorial
Matrix AdditionAdd two matrices.Try it
Matrix SubtractionSubtract matrices.Try it
Matrix MultiplicationRow×column product.Try it
Matrix DivisionElement-wise division.Try it
Matrix TransposeSwap rows and columns.Try it
Decimal to BinaryBase-10 → base-2.Try it
Binary to DecimalBase-2 → base-10.Try it
Decimal to OctalBase-10 → base-8.Try it
Pascal's TriangleBinomial coefficients.Try it
Amicable NumberFriendly divisor pairs.Try it

Edge Cases & Interview Pitfalls

Call these out before the interviewer asks—it shows Java tutorial maturity.

0

Zero and one

0 is even; 1 is not prime. Many number classifiers special-case small values.

-

Negatives

Ask whether the problem allows negatives. Digit loops often need Math.abs or an early reject.

scan

Unchecked Scanner

Prefer hasNextInt() before nextInt() so bad input does not throw mid-demo.

O(n)

Complexity one-liner

State big-O briefly: single-number checks are often O(√n) for primes; array scans are O(n).

Key Takeaways

  • Interview programs train loops, conditions, and Scanner I/O—not huge apps.
  • Start with even/odd, prime, digit sum, then arrays.
  • Explain first, then code—inputs, algorithm, edge cases, complexity.
  • Next: Even Number or browse the index above.

One line: Use this hub to pick a Java interview drill, practice the pattern, then open the full program tutorial for code and FAQs.

Frequently Asked Questions

They are short Java practice problems used in labs and fresher interviews—checking primes, digit sums, arrays, matrices, and number-base conversions. Each program trains loops, conditions, and Scanner/System.out I/O more than large application design.
Start with Even Number, Odd Number, and Prime Number. Then try Sum of Digits, Factorial, and Fibonacci Series. Move to arrays (sum/max/min) before matrix and conversion drills.
State the inputs and expected output, outline the algorithm (loops and conditions), mention edge cases (0, negatives, empty arrays), then walk through the code. Compile with javac and run with java when asked.
Use Scanner with hasNextInt() (or parse carefully) before nextInt(). Close the Scanner when you are done, or use try-with-resources in longer programs.
Definition, algorithm, complete Java code, sample output, walkthrough, and FAQs. Use this hub to pick a topic, then open the individual tutorial.
This hub is practice programs for interviews and labs. Java Strings teaches the String class, immutability, and related methods. Use both tracks together when you need APIs plus problem drills.

Start with Even Number

Open the first warm-up tutorial—definition, algorithm, full code, and FAQs.

Even Number tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful