This collection of basic Java programs is designed for beginners preparing for interviews or practicing core concepts.


1. Read Number From Standard Input

Description: Reads an integer from the user and prints it.

Logic: Use Scanner class → read with nextInt().

Program:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println("You entered: " + num);
    }
}

Input:

5

Output:

You entered: 5

2. Get Input from the User

Description: Reads a string (name) from the user.

Logic: Use Scanner with nextLine().

Program:

import java.util.Scanner;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("Hello, " + name);
    }
}

Input:

John

Output:

Hello, John

3. Multiply Two Floating-Point Numbers

Description: Multiply two decimal numbers.

Logic: Use float or double type with * operator.

Program:

class Main {
    public static void main(String[] args) {
        float a = 2.3f, b = 4.5f;
        float product = a * b;
        System.out.println("Product = " + product);
    }
}

Input:

a = 2.3, b = 4.5

Output:

Product = 10.35

4. Swap Two Numbers

Description: Swap values of two integers using a temporary variable.

Logic: Store a in temp, assign b to a, then temp to b.

Program:

class Main {
    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);
    }
}

Input:

a = 5, b = 10

Output:

a = 10, b = 5

5. Add Two Binary Strings

Description: Adds two binary numbers represented as strings.

Logic: Convert to decimal → add → convert back to binary.

Program:

class Main {
    public static void main(String[] args) {
        String a = "1010", b = "1101";
        int sum = Integer.parseInt(a, 2) + Integer.parseInt(b, 2);
        System.out.println(Integer.toBinaryString(sum));
    }
}

Input:

a = "1010", b = "1101"

Output:

10111

6. Add Two Complex Numbers

Description: Adds two complex numbers.

Logic: Add real parts and imaginary parts separately.

Program:

class Complex {
    int real, imag;
    Complex(int r, int i) { real = r; imag = i; }
    Complex add(Complex c) {
        return new Complex(this.real + c.real, this.imag + c.imag);
    }
    public static void main(String[] args) {
        Complex c1 = new Complex(2, 3);
        Complex c2 = new Complex(4, 5);
        Complex result = c1.add(c2);
        System.out.println("Sum = " + result.real + " + " + result.imag + "i");
    }
}

Input:

(2+3i), (4+5i)

Output:

Sum = 6 + 8i

7. Check Even or Odd Integer

Description: Determine if a number is even or odd.

Logic: If n % 2 == 0 → Even, else Odd.

Program:

class Main {
    public static void main(String[] args) {
        int num = 7;
        if (num % 2 == 0)
            System.out.println("Even");
        else
            System.out.println("Odd");
    }
}

Input:

7

Output:

Odd

8. Find Largest Among 3 Numbers

Description: Find the maximum of three numbers.

Logic: Use Math.max() or nested conditions.

Program:

class Main {
    public static void main(String[] args) {
        int a = 10, b = 20, c = 15;
        int max = Math.max(a, Math.max(b, c));
        System.out.println("Largest = " + max);
    }
}

Input:

a=10, b=20, c=15

Output:

Largest = 20

9. Find LCM of 2 Numbers

Description: Find the Least Common Multiple (LCM) of two numbers.

Logic: Formula → LCM(a, b) = (a × b) / GCD(a, b).

Program:

class Main {
    static int gcd(int a, int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
    public static void main(String[] args) {
        int a = 15, b = 20;
        int lcm = (a * b) / gcd(a, b);
        System.out.println("LCM = " + lcm);
    }
}

Input:

a = 15, b = 20

Output:

LCM = 60

10. Find GCD or HCF of 2 Numbers

Description: Find the Greatest Common Divisor (GCD) of two numbers.

Logic: Use Euclidean Algorithm.

Program:

class Main {
    static int gcd(int a, int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
    public static void main(String[] args) {
        int a = 36, b = 60;
        System.out.println("GCD = " + gcd(a, b));
    }
}

Input:

a = 36, b = 60

Output:

GCD = 12

11. Display All Prime Numbers from 1 to N

Description: Print all prime numbers up to a given number N.

Logic: A prime number is divisible only by 1 and itself.

Program:

class Main {
    public static void main(String[] args) {
        int n = 20;
        for (int i = 2; i <= n; i++) {
            boolean prime = true;
            for (int j = 2; j * j <= i; j++) {
                if (i % j == 0) {
                    prime = false;
                    break;
                }
            }
            if (prime) System.out.print(i + " ");
        }
    }
}

Input:

n = 20

Output:

2 3 5 7 11 13 17 19

12. Check Leap Year

Description: Check whether a given year is a leap year.

Logic: Leap year → divisible by 400 OR divisible by 4 but not by 100.

Program:

class Main {
    public static void main(String[] args) {
        int year = 2024;
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            System.out.println("Leap Year");
        else
            System.out.println("Not Leap Year");
    }
}

Input:

2024

Output:

Leap Year

13. Check Armstrong Number between Two Integers

Description: Armstrong number → sum of cubes of digits = original number.

Logic: Loop through numbers in range and check condition.

Program:

class Main {
    public static void main(String[] args) {
        int start = 100, end = 500;
        for (int num = start; num <= end; num++) {
            int sum = 0, temp = num;
            while (temp > 0) {
                int d = temp % 10;
                sum += d * d * d;
                temp /= 10;
            }
            if (sum == num) System.out.println(num);
        }
    }
}

Input:

100 to 500

Output:

153
370
371
407

14. Check whether the Input Number is a Neon Number

Description: Neon Number → sum of digits of square = original number.

Logic: Compute square → sum its digits → check equality.

Program:

class Main {
    public static void main(String[] args) {
        int n = 9, sq = n * n, sum = 0;
        while (sq > 0) {
            sum += sq % 10;
            sq /= 10;
        }
        if (sum == n)
            System.out.println("Neon Number");
        else
            System.out.println("Not Neon Number");
    }
}

Input:

9

Output:

Neon Number

15. Check whether Input Character is Vowel or Consonant

Description: Determine if a given character is a vowel or consonant.

Logic: Compare character with 'a', 'e', 'i', 'o', 'u' (case insensitive).

Program:

class Main {
    public static void main(String[] args) {
        char ch = 'e';
        if ("aeiouAEIOU".indexOf(ch) != -1)
            System.out.println("Vowel");
        else
            System.out.println("Consonant");
    }
}

Input:

e

Output:

Vowel

16. Find Factorial of a Number

Description: Calculate factorial of a given number.

Logic: Multiply numbers from 1 to n.

Program:

class Main {
    public static void main(String[] args) {
        int n = 5, fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
        }
        System.out.println("Factorial = " + fact);
    }
}

Input:

5

Output:

Factorial = 120

17. Find Even Sum of Fibonacci Series Till Number N

Description: Calculate sum of even Fibonacci numbers up to N.

Logic: Generate Fibonacci series → add only even terms.

Program:

class Main {
    public static void main(String[] args) {
        int n = 10, a = 0, b = 1, sum = 0;
        while (b <= n) {
            if (b % 2 == 0) sum += b;
            int next = a + b;
            a = b;
            b = next;
        }
        System.out.println("Even Fibonacci Sum = " + sum);
    }
}

Input:

N = 10

Output:

Even Fibonacci Sum = 10

18. Calculate Simple Interest

Description: Compute simple interest on given principal, rate, and time.

Logic: Formula → SI = (P × R × T) / 100.

Program:

class Main {
    public static void main(String[] args) {
        double p = 1000, r = 5, t = 2;
        double si = (p * r * t) / 100;
        System.out.println("Simple Interest = " + si);
    }
}

Input:

P=1000, R=5%, T=2

Output:

Simple Interest = 100.0

19. Calculate Compound Interest

Description: Compute compound interest on given principal, rate, and time.

Logic: Formula → CI = P × (1 + R/100)^T – P.

Program:

class Main {
    public static void main(String[] args) {
        double p = 1000, r = 5, t = 2;
        double ci = p * Math.pow((1 + r / 100), t) - p;
        System.out.println("Compound Interest = " + ci);
    }
}

Input:

P=1000, R=5%, T=2

Output:

Compound Interest = 102.5

20. Find the Perimeter of a Rectangle

Description: Calculate perimeter of a rectangle using length and width.

Logic: Formula → Perimeter = 2 × (length + width).

Program:

class Main {
    public static void main(String[] args) {
        int length = 10, width = 5;
        int perimeter = 2 * (length + width);
        System.out.println("Perimeter = " + perimeter);
    }
}

Input:

Length = 10, Width = 5

Output:

Perimeter = 30