To check whether a number is a Prime or Composite

A prime number is a whole number greater than 1 , whose only two  whole – number factors are 1 and itself . The first few prime numbers are as follows  2 ,3 ,5 ,7, 11, 13, 17, 19 , 23 , and so on .

All the whole numers greater than 1 which are not prime numbers are known as composite numbers . ie any number having factors more than 1 and itself . For example 4, 6, 8 , 9, 10 ,12 , 14 ,16 ,18 ,20.

import java.util.Scanner;

public class PrimeNumber{
public static void main(String[] args) {
int num ;
boolean isPrime = true;                                     // boolean data type is either true or false
System.out.println(“Enter any number : “);
Scanner sc = new Scanner(System.in);
num= sc.nextInt();                                         // taking input from user
for(int i=2;i<num /2;i++){                             // iterating till the value is half the num
if(num %i ==0){
isPrime =false;
break;
}
}
if(isPrime){
System.out.println(“The number “+num+” is a prime number .”);
}
else {
System.out.println(“The number “+num+” is a composite number .”);
}
}
}