Beginner Java Programs
These are the classic small programs every Java learner should be comfortable writing from scratch. They build confidence with loops, conditionals, and basic logic before moving to bigger topics.
Why practice these?
These are the classic small programs every Java learner should be comfortable writing from scratch. They build confidence with loops, conditionals, and basic logic before moving to bigger topics.
Program 1: Check if a Number is Even or Odd
javapublic class EvenOdd { public static void main(String[] args) { int number = 15; if (number % 2 == 0) { System.out.println(number + " is even"); } else { System.out.println(number + " is odd"); } } }
Output:
15 is oddProgram 2: Find the Factorial of a Number
javapublic class Factorial { public static void main(String[] args) { int number = 5; int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; } System.out.println("Factorial of " + number + " is " + factorial); } }
Output:
Factorial of 5 is 120Program 3: Check if a Number is Prime
javapublic class PrimeCheck { public static void main(String[] args) { int number = 29; boolean isPrime = true; if (number <= 1) { isPrime = false; } else { for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { isPrime = false; break; } } } System.out.println(number + " is prime: " + isPrime); } }
Output:
29 is prime: trueKey Points to Remember
- Start with small, self-contained programs before attempting larger multi-class projects.
- Practicing the same type of problem in a few different ways builds stronger logic-building skills.
- These beginner programs are frequently asked in college assignments and entry-level interviews.