Java Coding Exercises
100%

Java Coding Exercises

Practice is one of the best ways to learn Java programming. In this lesson, you will solve coding exercises covering variables, operators, conditions, loops, arrays, strings, and methods.

Practice Tip: First try to solve each problem yourself. Then compare your solution with the example answer. Run each program in your Java compiler or IDE.

1. Print Hello World

Question: Write a Java program to print Hello World.

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

Output:

Hello World

2. Add Two Numbers

Question: Add two numbers and display the result.

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

        int sum = a + b;

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

Output:

Sum = 30

3. Find the Square of a Number

Question: Find the square of a number.

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

        int square = number * number;

        System.out.println("Square = " + square);
    }
}

Output:

Square = 25

4. Check Even or Odd

Question: Check whether a number is even or odd.

public class Main {
    public static void main(String[] args) {
        int number = 7;

        if (number % 2 == 0) {
            System.out.println("Even Number");
        } else {
            System.out.println("Odd Number");
        }
    }
}

Output:

Odd Number

The modulus operator % returns the remainder. If the remainder after division by 2 is zero, the number is even.

5. Find the Largest of Two Numbers

Question: Find the larger of two numbers.

public class Main {
    public static void main(String[] args) {
        int a = 25;
        int b = 40;

        if (a > b) {
            System.out.println(a + " is largest");
        } else if (b > a) {
            System.out.println(b + " is largest");
        } else {
            System.out.println("Both numbers are equal");
        }
    }
}

Output:

40 is largest

6. Check Positive, Negative, or Zero

Question: Identify whether a number is positive, negative, or zero.

public class Main {
    public static void main(String[] args) {
        int number = -5;

        if (number > 0) {
            System.out.println("Positive Number");
        } else if (number < 0) {
            System.out.println("Negative Number");
        } else {
            System.out.println("Zero");
        }
    }
}

Output:

Negative Number

7. Print Numbers from 1 to 10

Question: Use a for loop to print numbers from 1 to 10.

public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
    }
}

Output:

1
2
3
4
5
6
7
8
9
10

8. Print a Multiplication Table

Question: Print the multiplication table of 5.

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

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

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

9. Calculate the Factorial

Question: Calculate the factorial of 5.

public class Main {
    public static void main(String[] args) {
        int number = 5;
        long factorial = 1;

        for (int i = 1; i <= number; i++) {
            factorial = factorial * i;
        }

        System.out.println("Factorial = " + factorial);
    }
}

Output:

Factorial = 120

Factorial of 5 is \(5 \times 4 \times 3 \times 2 \times 1 = 120\). The factorial of zero is also 1.

10. Find the Sum of Natural Numbers

Question: Calculate the sum of numbers from 1 to 10.

public class Main {
    public static void main(String[] args) {
        int sum = 0;

        for (int i = 1; i <= 10; i++) {
            sum += i;
        }

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

Output:

Sum = 55

11. Reverse a Number

Question: Reverse the digits of a positive integer.

public class Main {
    public static void main(String[] args) {
        int number = 1234;
        int reverse = 0;

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

        System.out.println("Reverse = " + reverse);
    }
}

Output:

Reverse = 4321

The modulus operator extracts the last digit, and integer division removes that digit from the original number.

12. Check a Palindrome Number

Question: Check whether a positive integer reads the same forwards and backwards.

public class Main {
    public static void main(String[] args) {
        int number = 121;
        int original = number;
        int reverse = 0;

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

        if (original == reverse) {
            System.out.println("Palindrome Number");
        } else {
            System.out.println("Not a Palindrome");
        }
    }
}

Output:

Palindrome Number

13. Find the Largest Element in an Array

Question: Find the largest value in an integer array.

public class Main {
    public static void main(String[] args) {
        int[] numbers = {10, 25, 7, 40, 15};

        int largest = numbers[0];

        for (int number : numbers) {
            if (number > largest) {
                largest = number;
            }
        }

        System.out.println("Largest = " + largest);
    }
}

Output:

Largest = 40

14. Count Vowels in a String

Question: Count the vowels in a string.

public class Main {
    public static void main(String[] args) {
        String text = "Hello Java";
        int count = 0;

        for (int i = 0; i < text.length(); i++) {
            char ch = Character.toLowerCase(text.charAt(i));

            if (ch == 'a' || ch == 'e' || ch == 'i'
                    || ch == 'o' || ch == 'u') {
                count++;
            }
        }

        System.out.println("Vowels = " + count);
    }
}

Output:

Vowels = 4

15. Create a Method to Add Two Numbers

Question: Create a method that accepts two integers and returns their sum.

public class Main {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        int result = add(15, 25);

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

Output:

Sum = 40

The add() method accepts two parameters and returns the calculated sum.

16. Check Whether a Number Is Prime

Question: Check whether a number is prime.

public class Main {
    public static void main(String[] args) {
        int number = 17;
        boolean isPrime = number >= 2;

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

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

Output:

Prime Number

A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.

17. Swap Two Numbers

Question: Swap the values of two variables using a temporary variable.

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

        System.out.println("Before: a = " + a + ", b = " + b);

        int temp = a;
        a = b;
        b = temp;

        System.out.println("After: a = " + a + ", b = " + b);
    }
}

Output:

Before: a = 10, b = 20
After: a = 20, b = 10

18. Count Digits in a Number

Question: Count the digits in a non-negative integer.

public class Main {
    public static void main(String[] args) {
        int number = 12345;
        int count = 0;

        do {
            count++;
            number = number / 10;
        } while (number != 0);

        System.out.println("Digits = " + count);
    }
}

Output:

Digits = 5

The do-while loop executes at least once, so it also counts zero as a one-digit number.

19. Additional Coding Challenges

Try to solve these questions without looking at the solutions above.

  1. Find the smallest of three numbers.
  2. Print all even numbers from 1 to 100.
  3. Print the Fibonacci series up to 10 terms.
  4. Calculate the sum of digits of a number.
  5. Check whether a year is a leap year.
  6. Sort an integer array in ascending order.
  7. Find the second-largest element in an array.
  8. Reverse a string.
  9. Count the words in a sentence.
  10. Build a simple calculator using a switch statement.
  11. Create a Student class with name and age fields.
  12. Store and display student names using an ArrayList.

Java Coding Exercises - Quick Quiz

Choose the correct answer for each question.

1. Which operator returns the remainder after division?



2. Which loop is useful when the number of repetitions is known?



3. Which keyword returns a value from a method?