Java Program: Program for checking the no. is prime or not and the approach towards the problem....
Published: 2022-11-06 10:18:52

**There are many approaches out there in the internet but this seems to be simple and easy to grasp and with less complexity , and you will find this approach very interesting let's get into the approach.**

#Check whether no. is prime or not?# 
_So What is a prime no.? => Prime numbers are those which are divisible by 1 or itself... so 3,5,7,11,13,17 and so on ......... upto -n are prime numbers **

So the approach is if we have to check from 2 to that number for example we have given a number 5 and we have to check 5 is prime or not so we have to check from 2 to 4 like 2,3,4 can divide the no. if it divides the number it is not prime ,so what's the definition of prime prime numbers are those which are divisible by itself or 1 so if any other number easily divides the number it is not a prime number .. simple thing... don't worry I will provide the code snippets also and provides some images to get full grasp .**

So now the question comes up that if we get more than 4 digits number so we have to check with between 1 to that number so we have to check for 2 to that number and it will take more time because it will check between and take more time .. so to solve this issue we can do is we can check for the square root of that number so it will reduce the time and complexity for example => if we have check for 4759474 with this number you are able to see how much time will it will take and we have to perform with every number form 2 to that number . so if we do like like 2*2 <= SquareRoot of number. like 22 <= square root of 14. and if we have this bigger number we will check the square root of this number 4759470.5=2181.62187374 so if have to check with the number it will start from 2 to 47546 it is lengthy and take more time and if we check with square root of 4759470.5=2181.62187371 .. so you can see clearly the complexity we have to check this 2181.62187371 times. see this how much the complexity reduced with this simple Maths **

So the Questions why we are checking with square root of that number ... Let me give you the example_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v032yt1omipwilbk4ryc.jpeg)

>So in the above image check the repetition of number 94 123 give us 36 so do we need to check with same again and again that why we are just checking with the square root of that number because it is same as previous number ... so to avoid repeating the same number again and again we are just checking with square root . Take any number there will be some repetitions going on so we don't need to check with the same number again and again... Hope you understand.

```
import java.util.Scanner;

public class Prime {
    public static void main(New[] args) {
        System.out.println("Prime No Finding Program");
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the No.");
        int number = scan.nextInt();
        if(number<=1){
            System.out.println("Neither prime nor composite");
        }
        int count = 2;
   while(count*count<=number){
    if(number%count==0){


        System.out.println("not a prime number "+ number);
        return;
    }
    count ++;

   }
        if(count>=number){
            System.out.println("prime number");
        }

    }
}
```


> I hope this was helpful to at least, one of you. And as always -

Happy Coding! 💖👩‍💻

And Special Thanks to Kunal Kushwaha for making this topic ... Great guy.... I'm providing the link of his channel plz visit u will be amazed with top notch content .. Kunal Kushwaha

Please comment below for any other suggestion ....
`