Java Program: Program for checking the no. is prime or not and the approach towards the problem....

There are many ways to Check the no. is prime or not.... But I assure you this is the best approach to find the prime with less complexity.

ยท

3 min read

Java Program: Program for checking the no. is prime or not and the approach towards the problem....

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

WhatsApp Image 2022-11-06 at 00.21.12.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.

Providing the code below for better understanding


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");
        }

    }
}

github

Screenshot (753).png

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 ....

ย