Java Program to Check if a Number is Odd or Even
Uses modulo % 2 to determine parity.
public class Main {
public static void main(String[] args) {
int n = 7;
System.out.println((n % 2 == 0) ? "even" : "odd");
}
}odd
Key takeaway: Practice Java with 33+ clean, runnable examples covering arrays, strings, classes, interfaces, exceptions, collections, and streams — all with expected output to verify instantly.
Java is a robust, object-oriented programming language. These examples are designed for students, interview candidates, backend engineers, and developers learning Java hands-on.
Last updated: October 2026 · 33 examples
Who is this for: CS students, job seekers preparing for Java interviews, self-taught coders, and developers transitioning to Java. Use this when: you need quick syntax reference, practice problem-solving, or build foundational fluency.
Related:Java Compiler · Python Examples · JavaScript Examples · TypeScript Examples
Uses modulo % 2 to determine parity.
public class Main {
public static void main(String[] args) {
int n = 7;
System.out.println((n % 2 == 0) ? "even" : "odd");
}
}odd
Prints a greeting to the console using System.out.println.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Hello, World!
Adds two integers and prints their sum.
public class Main {
public static void main(String[] args) {
int a = 12;
int b = 30;
int sum = a + b;
System.out.println(sum);
}
}42
Computes square root using Math.sqrt.
public class Main {
public static void main(String[] args) {
double n = 81.0;
System.out.println(Math.sqrt(n));
}
}9.0
Uses the formula 1/2 * base * height.
public class Main {
public static void main(String[] args) {
double base = 10, height = 5;
double area = (base * height) / 2.0;
System.out.println(area);
}
}25.0
Swaps variables using a temporary variable.
public class Main {
public static void main(String[] args) {
int a = 3, b = 4;
int temp = a;
a = b;
b = temp;
System.out.println(a + " " + b);
}
}4 3
Computes roots using the quadratic formula.
public class Main {
public static void main(String[] args) {
double a = 1, b = -3, c = 2;
double d = b*b - 4*a*c;
double r1 = (-b + Math.sqrt(d)) / (2*a);
double r2 = (-b - Math.sqrt(d)) / (2*a);
System.out.println(r1 + " " + r2);
}
}2.0 1.0
Converts km to miles (1 km ≈ 0.621371 mi).
public class Main {
public static void main(String[] args) {
double km = 10.0;
double miles = km * 0.621371;
System.out.println(miles);
}
}6.21371
Uses the formula (C*9/5)+32.
public class Main {
public static void main(String[] args) {
double c = 25.0;
double f = (c * 9/5) + 32;
System.out.println(f);
}
}77.0
Checks the sign of a number using conditionals.
public class Main {
public static void main(String[] args) {
int n = 0;
if (n > 0) System.out.println("positive");
else if (n < 0) System.out.println("negative");
else System.out.println("zero");
}
}zero
Computes factorial using iterative loop.
public class Main {
public static void main(String[] args) {
int n = 5;
long fact = 1;
for (int i = 2; i <= n; i++) fact *= i;
System.out.println(fact);
}
}120
Prints first N Fibonacci numbers.
public class Main {
public static void main(String[] args) {
int n = 7;
int a = 0, b = 1;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(a);
if (i < n-1) sb.append(" ");
int next = a + b;
a = b;
b = next;
}
System.out.println(sb.toString());
}
}0 1 1 2 3 5 8
Determines if a number is prime.
public class Main {
static boolean isPrime(int n) {
if (n < 2) return false;
if (n % 2 == 0) return n == 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(29));
}
}true
Counts vowels using regex.
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String s = "Hello World";
Matcher m = Pattern.compile("[aeiouAEIOU]").matcher(s);
int count = 0;
while (m.find()) count++;
System.out.println(count);
}
}3
Use our free online Java compiler to run all examples, test code variations, and debug errors in your browser.
javac Main.java and run with java Main. On macOS, Linux, or Windows, use the official OpenJDK builds or your package manager. Java powers enterprise applications, Android development, and countless backend systems. Practicing small, focused programs builds fluency with syntax and core concepts such as data types, control flow, methods, classes, interfaces, exceptions, collections, and streams.
On this page, you will find clean, well-formatted examples that run across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.
Whether you are preparing for interviews, working through coursework, or refreshing fundamentals, these examples offer a practical path to mastering Java. Bookmark the page and return often to keep improving.
This collection of Java examples covers fundamental to intermediate concepts with clean, runnable code. Each program is self-contained — copy it into any Java IDE, online compiler, or your local development environment and run it immediately. The examples progress from basic syntax through object-oriented programming, data structures, and algorithms.
Java remains one of the most popular programming languages worldwide, ranking consistently in the top 3 on TIOBE, Stack Overflow, and GitHub. It powers Android apps, enterprise backends (Spring Boot), big data (Hadoop), and financial systems. Practising with small, focused examples is the fastest way to build Java fluency.
Research on skill acquisition shows that deliberate practice — focused, repetitive work on specific concepts — produces faster learning than reading alone. Small Java programs isolate one concept at a time, making it easy to understand, modify, and remember. This is why coding interviews test with small problems: they reveal fundamental fluency that large project experience alone cannot demonstrate.
Each example on this page is designed to be completed in 2–5 minutes, making them perfect for daily practice sessions, interview preparation, or quick reference when you forget a specific syntax pattern.
Most examples use Java 8+ features. Some advanced examples use Java 17 features like records and text blocks. All run on the current LTS (Java 17 or 21).
Yes. The examples cover the most common interview topics: arrays, strings, recursion, OOP, collections, and basic algorithms. Practice modifying them to solve variations.
In Java, the class name must match the filename. Using Main makes examples portable across online compilers that expect this convention. In real projects, you would name classes descriptively.
Yes, completely free. Copy, modify, and use them in your projects, homework, or learning materials without attribution.
CS students practising for exams and assignments. Job seekers preparing for Java technical interviews. Self-taught developers building foundational Java skills. Teachers looking for clean, copyable code examples for lessons. Android developers refreshing core Java knowledge. Backend engineers transitioning from other languages to Java/Spring Boot.