**AIM:
– **Write a program
to check whether a given number is prime.

**Contents**hide

**Prime Number:**

** **A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 Ã— 5 or 5 Ã— 1, involve 5 itself.

Here we are writing about Write a program to check whether a given number is prime in Java. Our initiative is that user understand code by reading comment so that he/she can became capable to any understand code in this programming world.

**Source Code: –**

import java.util.Scanner; //Prime number is a number that is divisble only by 1 and itself public class Prime { public static void main(String[] args) { int number; Scanner scanner = new Scanner(System.in); System.out.print("Enter the number:- "); number = scanner.nextInt(); /*For finding a prime number we start the loop with initial value of i as 2 * and keep on checking the divisibility of the number by i till i reaches half the * value of the given number. If the number is divisible then the message is printed * and the method returns. */ for(int i=2; i<=number/2; i++) { if(number % i == 0) { System.out.println("The given number " + number + " is not prime"); return; } } //This statement will be printed when none of the values of i divide the number completely System.out.println("The given number " + number + " is prime"); } }

**Output:**

## 0 Comments