Easy Java Code Snippets

Explore these Java code examples that you can easily copy and use in your own projects.

Basic Hello World

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

Simple For Loop

    for (int i = 0; i < 10; i++) {
        System.out.println(i);
    }
                

Basic Calculator

import java.util.Scanner;

public class BasicCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter first number: ");
        double num1 = scanner.nextDouble();
        System.out.print("Enter second number: ");
        double num2 = scanner.nextDouble();
        System.out.print("Enter an operator (+, -, *, /): ");
        char operator = scanner.next().charAt(0);
        double result = 0;

        switch (operator) {
            case '+': result = num1 + num2; break;
            case '-': result = num1 - num2; break;
            case '*': result = num1 * num2; break;
            case '/': result = num1 / num2; break;
        }
        System.out.println("Result: " + result);
    }
}
                    

Temperature Converter

import java.util.Scanner;

public class TemperatureConverter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter temperature in Celsius: ");
        double celsius = scanner.nextDouble();
        double fahrenheit = (celsius * 9/5) + 32;
        System.out.println(celsius + " Celsius = " + fahrenheit + " Fahrenheit");
    }
}
                    

Guess the Number Game

import java.util.Random;
import java.util.Scanner;

public class GuessTheNumber {
    public static void main(String[] args) {
        Random random = new Random();
        int number = random.nextInt(100) + 1;
        Scanner scanner = new Scanner(System.in);
        int guess;
        do {
            System.out.print("Guess a number between 1 and 100: ");
            guess = scanner.nextInt();
            if (guess < number) System.out.println("Higher");
            else if (guess > number) System.out.println("Lower");
        } while (guess != number);
        System.out.println("Correct! The number was " + number);
    }
}
                    

Simple ATM Interface

import java.util.Scanner;

public class SimpleATM {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int balance = 1000; // initial balance
        System.out.println("Welcome to Simple ATM");
        System.out.println("1: Check Balance");
        System.out.println("2: Withdraw");
        System.out.println("3: Deposit");
        System.out.print("Choose an option: ");
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                System.out.println("Your balance is: " + balance);
                break;
            case 2:
                System.out.print("Enter amount to withdraw: ");
                int withdraw = scanner.nextInt();
                if (withdraw <= balance) balance -= withdraw;
                else System.out.println("Insufficient balance.");
                break;
            case 3:
                System.out.print("Enter amount to deposit: ");
                int deposit = scanner.nextInt();
                balance += deposit;
                break;
        }
    }
}
                    

Palindrome Checker

import java.util.Scanner;

public class PalindromeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = scanner.nextLine();
        String reverse = new StringBuilder(str).reverse().toString();
        if (str.equals(reverse)) System.out.println(str + " is a palindrome");
        else System.out.println(str + " is not a palindrome");
    }
}
                    

Simple Interest Calculator

import java.util.Scanner;

public class SimpleInterestCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Principal amount: ");
        double principal = scanner.nextDouble();
        System.out.print("Enter Annual Interest Rate: ");
        double rate = scanner.nextDouble();
        System.out.print("Enter Time (years): ");
        double time = scanner.nextDouble();
        double interest = (principal * rate * time) / 100;
        System.out.println("Simple Interest: " + interest);
    }
}
                    

Factorial Calculator

import java.util.Scanner;

public class FactorialCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        int factorial = 1;
        for (int i = 1; i <= num; i++) {
            factorial *= i;
        }
        System.out.println("Factorial of " + num + " is " + factorial);
    }
}
                    

Fibonacci Series Generator

import java.util.Scanner;

public class FibonacciSeries {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter number of terms: ");
        int count = scanner.nextInt();
        int n1 = 0, n2 = 1;
        System.out.print("First " + count + " terms: ");
        for (int i = 1; i <= count; ++i) {
            System.out.print(n1 + " ");
            int sum = n1 + n2;
            n1 = n2;
            n2 = sum;
        }
    }
}
                    

Prime Number Checker

import java.util.Scanner;

public class PrimeChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        boolean flag = false;
        for (int i = 2; i <= num / 2; ++i) {
            if (num % i == 0) {
                flag = true;
                break;
            }
        }
        if (!flag) System.out.println(num + " is a prime number.");
        else System.out.println(num + " is not a prime number.");
    }
}