Write a Java program to check if a given number is prime or not.

Here’s a Java program to check if a given number is prime or not:

import java.util.Scanner;

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

In this program, we first take user input for the number to be checked. We then initialize a boolean variable `isPrime` to true, which will be used to keep track of whether the number is prime or not.

We then check if the number is less than or equal to 1, in which case it cannot be prime, so we set `isPrime` to false. Otherwise, we use a for loop to check if the number is divisible by any integer from 2 to half the number (inclusive). If it is, then the number is not prime, so we set `isPrime` to false and break out of the loop.

At the end of the program, we check the value of `isPrime`. If it is true, then the number is prime, and we print a message to that effect. Otherwise, the number is not prime, and we print a message to that effect.