15 Java Programs for Beginners

    Atul Kabra6 min readUpdated

    The fastest way to learn Java is to write small programs that each practise one idea. Below are 15 beginner programs with complete, commented code: number checks, loops, strings, and simple algorithms. Type each one out (do not just copy), compile it, and run it. Together they exercise variables, operators, control flow, arrays, and methods — the core skills from the rest of this tutorial cluster.

    1. Even or odd

    public class EvenOdd {
        public static void main(String[] args) {
            int n = 7;
            // % gives the remainder; an even number divides by 2 with no remainder.
            System.out.println(n % 2 == 0 ? "Even" : "Odd"); // Odd
        }
    }
    

    2. Sum of first N numbers

    public class SumN {
        public static void main(String[] args) {
            int n = 5, sum = 0;
            for (int i = 1; i <= n; i++) sum += i; // accumulate 1+2+3+4+5
            System.out.println("Sum = " + sum);    // Sum = 15
        }
    }
    

    3. Factorial

    public class Factorial {
        public static void main(String[] args) {
            int n = 5;
            long fact = 1;                       // use long: factorials grow fast
            for (int i = 2; i <= n; i++) fact *= i;
            System.out.println(n + "! = " + fact); // 120
        }
    }
    

    4. Largest of three numbers

    public class Largest {
        public static void main(String[] args) {
            int a = 12, b = 27, c = 19;
            int max = Math.max(a, Math.max(b, c)); // Math.max compares two at a time
            System.out.println("Largest = " + max); // 27
        }
    }
    

    5. Reverse a number

    public class ReverseNumber {
        public static void main(String[] args) {
            int n = 1234, reversed = 0;
            while (n != 0) {
                reversed = reversed * 10 + n % 10; // peel off the last digit
                n /= 10;
            }
            System.out.println(reversed); // 4321
        }
    }
    

    6. Palindrome check (string)

    public class Palindrome {
        public static void main(String[] args) {
            String s = "level";
            String reversed = new StringBuilder(s).reverse().toString();
            System.out.println(s.equals(reversed) ? "Palindrome" : "Not"); // Palindrome
        }
    }
    

    7. Count vowels in a string

    public class CountVowels {
        public static void main(String[] args) {
            String s = "Jalgaon";
            int count = 0;
            for (char ch : s.toLowerCase().toCharArray()) {
                if ("aeiou".indexOf(ch) >= 0) count++; // is the char a vowel?
            }
            System.out.println("Vowels = " + count); // 3
        }
    }
    

    8. Prime number check

    public class PrimeCheck {
        public static void main(String[] args) {
            int n = 29;
            boolean prime = n > 1;
            for (int i = 2; i * i <= n; i++) { // check up to the square root
                if (n % i == 0) { prime = false; break; }
            }
            System.out.println(prime ? "Prime" : "Not prime"); // Prime
        }
    }
    

    9. Fibonacci series

    public class Fibonacci {
        public static void main(String[] args) {
            int a = 0, b = 1;
            System.out.print(a + " " + b);
            for (int i = 2; i < 8; i++) {
                int next = a + b;          // each term is the sum of the previous two
                System.out.print(" " + next);
                a = b; b = next;
            }
            // 0 1 1 2 3 5 8 13
        }
    }
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    10. Multiplication table

    public class Table {
        public static void main(String[] args) {
            int n = 7;
            for (int i = 1; i <= 10; i++) {
                System.out.println(n + " x " + i + " = " + (n * i));
            }
        }
    }
    

    11. Swap two numbers (without a temp variable)

    public class Swap {
        public static void main(String[] args) {
            int a = 5, b = 9;
            a = a + b; // a = 14
            b = a - b; // b = 5
            a = a - b; // a = 9
            System.out.println("a=" + a + ", b=" + b); // a=9, b=5
        }
    }
    

    12. Array sum and average

    public class ArrayStats {
        public static void main(String[] args) {
            int[] marks = {88, 92, 75, 60};
            int sum = 0;
            for (int m : marks) sum += m;
            System.out.println("Average = " + (double) sum / marks.length); // 78.75
        }
    }
    

    13. Find the maximum in an array

    public class ArrayMax {
        public static void main(String[] args) {
            int[] data = {12, 45, 7, 89, 33};
            int max = data[0];
            for (int v : data) if (v > max) max = v; // keep the biggest seen so far
            System.out.println("Max = " + max); // 89
        }
    }
    

    14. FizzBuzz

    public class FizzBuzz {
        public static void main(String[] args) {
            for (int i = 1; i <= 15; i++) {
                if (i % 15 == 0) System.out.println("FizzBuzz");
                else if (i % 3 == 0) System.out.println("Fizz");
                else if (i % 5 == 0) System.out.println("Buzz");
                else System.out.println(i);
            }
        }
    }
    

    15. Simple calculator with a method

    public class Calculator {
        // A reusable method that returns a result.
        static double apply(double a, double b, char op) {
            return switch (op) {           // modern switch expression
                case '+' -> a + b;
                case '-' -> a - b;
                case '*' -> a * b;
                case '/' -> b != 0 ? a / b : Double.NaN; // guard against divide-by-zero
                default  -> Double.NaN;
            };
        }
        public static void main(String[] args) {
            System.out.println(apply(6, 3, '*')); // 18.0
            System.out.println(apply(6, 0, '/')); // NaN
        }
    }
    

    Common mistakes

    • Copying without typing. You learn by typing and fixing your own errors, not by pasting.
    • Integer division surprises. In program 12, casting to double is what makes the average correct.
    • Wrong loop bounds. Decide whether you want < or <=; an off-by-one error is the most common bug here.
    • Comparing Strings with ==. In the palindrome program, use .equals(), never ==.
    • Skipping the divide-by-zero guard. Program 15 checks b != 0 for a reason.

    FAQ

    What should I practise after these? Re-solve each one a second way — for example, write factorial recursively, or read the input from the keyboard with a Scanner.

    How do I run these? Save each in a file matching the class name (e.g. Factorial.java), then javac Factorial.java and java Factorial. See the JDK setup guide.

    Keep going

    Strengthen the fundamentals behind these programs with Control Flow in Java and Arrays in Java. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.


    Practise dozens more programs with mentor feedback at Infoplanet, Jalgaon. Join the waitlist for the Java course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides