 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you how you can check whether the entered number is prime or not in Java. First of all I am going to import a package whose name is utl. Inside this package I have imported a class whose name is scanner. It is predefined class which is available in utl package. Scanner class is required to read input from user. That's why I have imported it. Now I am going to define another class whose name is prime. Inside this class I am going to define main method. Inside this main method I am going to implement the logic which will check whether the entered number is prime or not. So I am going to declare some variables. Now I am going to create instance of scanner class which will be required to receive input from user and the system.in is passed as constructor value of scanner class which will decide that the input will be received from the user through console. Now I am going to write system.out.println. Through this I am going to print a message, enter a number on output screen. After printing this message user will enter a particular number. So I am going to receive that number through next int method. So next int method is already defined in scanner class and the entered number will be stored inside n variable. Now I hope you have understood why I have imported scanner class. Now I am going to implement a loop. You can see here I have started loop from 2 and its condition is i less than n. So in case of prime number we know that the number which is divisible by 1 and itself then the number is prime otherwise it is not prime. So I cannot start loop from 1 because this number is divisible by 1 and I cannot repeat this loop till n because number is divisible by itself. So that's why I have started loop from 2 and I am going to repeat it till less than n. Now I have to implement if condition. So see this condition it is n modulus i. So I am dividing n every time by value of i. If it is divisible completely then its value will be equals to zero. If it is true I am going to change value of flag by 1 and I am going to terminate the loop with the help of break. So now you have to repeat this loop. Suppose value of n is 7. For example we are taking value of n as 7. So loop is starting from 2. So first time if we will try to divide 7 by 2 so it will not be divisible. Again I will increment it. Value of i becomes 3. If will be checked 7 is not divisible by 3. So this way every time n will be divisible by a particular number which is available inside i variable. So 7 is not divisible by 3, 4, 5 and 6. So this if condition which is available at line number 13 will not be true in case of 7. So value of flag will remain zero. Now if n is 6 then in that case 6 is divisible by 2. So this if condition will become true in first iteration and value of flag will become 1. It means 6 is not a prime number. So after completion of loop we can apply another if which will check value of flag. So if flag remains zero it means number is prime otherwise number is not prime. So here I can print the message number is prime else I can print number is not prime. So flag is a normal variable which is responsible here for deciding whether the number is prime or not. I hope you have understood how I have implemented this concept in front of you. Now you can also see I have used break. Why I have used break in case of 6 if value of n is 6 the number is divisible by in first iteration itself. So if number is divisible at a particular moment then we don't have to repeat loop unnecessarily. So if you want to break a particular loop when a particular condition is true then you can use this break keyword inside the loop. So if you want to stop execution of a loop in a particular step then you can apply break. So I have applied break with this if condition. So whenever this if condition is true then this break will be executed and the repetition of loop will be terminated. Now I am going to save this code. I have already saved this file with prime.java. You can see its name and this file is available in D drive in Java folder. So after saving this file I can compile it and I can execute it. So for that purpose I am going to open command prompt. Here I am going to D drive then inside Java folder. So you can see my current location is D Java. After moving to Java folder I have to compile the program. So I have typed java c space prime.java. So this command will compile the code. It will see whether I have made any mistake or not. So there are no error masses. It means program is written correctly. It is not having any syntax error. After compilation I can run the program. So to execute the program I am typing java space name of byte code that is prime and I am pressing enter key. So now I am entering 7. So 7 is a prime number. You can see it is showing the output number is prime. Again I am going to execute the program. This time I am going to enter 6. You can see it is showing number is not prime. So this way I have demonstrated you how you can check whether the number is prime or not in Java. I hope you have understood whatever I have explained you. If you want to watch more programs or more programming related videos you can follow my YouTube channel. Thank you for watching this video.