 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can check whether the number is prime or not. So this program I am going to implement in C programming. And before starting of the explanation, I just want to share one information. So if you go into the video description of this video, so you will find various links related to playlists that are available on my channel. So you can open those links and you can watch various programming related videos. So now I am going to start the explanation. So first I am going to explain what prime number is. So let's say we have a variable and whose value is 7. Now we need to check whether this 7 is prime or not. So the numbers which divides by 1 and itself the numbers which are divisible by 1 and itself only those numbers are treated as prime numbers. So if we take 7, so we have these numbers 1, 2, 3, 4, 5, 6 and 7. We have these 7 numbers from which this 7 can be divisible. So here you can see if we try to divide 7 with these digits individually. So 7 will be divisible by 1 and 7 only. It is not possible to divide 7 with these numbers. So 7 fulfills the requirement of prime number that the number should be divisible by 1 or self. So 7 is divisible by 1 and 7 is divisible by itself. So that's why this 7 is treated as prime number. Now if we take one more example, let's say number is 6. So it can be divisible by these 6 numbers. So now we need to identify actually from which number 6 can be divisible. So we can divide 6 by 1, we can divide 6 by 2, we can divide 6 by 3 and we can divide 6 by 6. So other than 1 and self, 6 is divisible by 2 more numbers that are 2 and 3. So it means 6 is not considered as a prime number. So 7 is a prime number but 6 is not a prime number. So now we need to implement the logic for this so that we can receive a number as input from the user. And then we need to check whether that number is divisible by 1 or self or it is divisible by other numbers as well. So if it is divisible by 1 or self then that can be considered as prime otherwise not. So I hope you understood the logic how we can check whether the number is prime or not. So this was mathematical explanation. Now we need to convert this explanation into C programming. So before writing the logic, I am just writing essential statements. So name, then variable n, then one variable I am going to use as flag. Its initial value is 0. One more variable I am going to use as i. Then I am writing printf, enter a number, then scanf, percent d and percent n. So this way with the help of this printf scanner we can read the number whose we want to check for prime and n, flag and i these variables I am going to use. Now let's try to implement the logic. So here we need to divide this number again and again by several digits. So it means it is sure we need to implement a loop. So I am going to use for loop. So inside for loop we have three parts initialization, termination condition and increment decrement. So we need to decide all three parts. So I am taking initial value as 2. Why so? Because every number will be divisible by one. So we are not trying to divide the number by one. So we are starting the loop from 2. Now termination condition will be i less than n. So we will repeat the loop till less than n. So let's say if number is 7. So we will start the loop from 2 and we will stop it to 6. Why so? Because we are removing one because from one every number will be divisible and we are removing the cells number because number is always divisible by itself. So we are not considering both the cases. Beginning that is 1 and ending that is the number itself. Or here you can also write i less than equals to n minus 1. So you can also implement this. Both are same. Then increment will be i plus plus because we want to increase i by 1. So this way I implemented this loop which is very simple now. Starting from 2 and ending to number minus 1. Now inside this for loop I am going to apply if condition. So this will be life. So here try to understand this condition carefully. So variable is n which is divisible always and it will be divided by i. So n will be fixed. Let's say example number is 7. So loop is started from 2. 2 is less than 7 which is 2. Then 7 we are trying to divide it by 2. So if it is divisible then its remainder will be 0 or otherwise not. Then loop will increment so i will become 3. So then again 7 will be divided by 3. Then 7 will be divided by 4 and so on. So this way n will remain fixed and i will be incremented by 1 by 1. And this remainder will be compared to 0. If it is true then further steps will be performed otherwise not. So if number is divisible by i. If number is divisible by i it means it is divisible by other than the number 1 or itself. It is divisible by the number which is other than 1 or itself. It means here we can print number is not prime and else if it is not divisible we can print prime. So initially you will think look like this but it is not correct. It is totally incorrect. So let me explain why. So we are taking example as 7. Loop is starting from 2. So 7 modulus 2. Is it divisible by 2 or not? So we are going to as part so it will print prime. Then loop will increment so value of i will become 3. So 7 modulus 3. So now it is divisible. Again it will print prime. Then for 4 again it will print prime. So for 2, 3, 4, 5, 6. So 5 times it will print prime. That is not correct. Now take example as 6. So 6 it will be divided by 2 first. So 6 is completely divisible by 2. So it will print not prime. Then it will become 3. 6 is divisible by 3 as well. So it will print not prime. Then i will become 4. So 6 is not divisible by 4. So it will print prime. So here you can see for 2 and 3 it is printing not prime. For 4 and 5 it is printing prime. So that is also incorrect. It means inside this loop we cannot decide whether the number is prime or not. Because we need to divide that number by all the digits first. And after completion of this loop we can decide whether the number is prime or not. So now I am removing these statements because these are not correct. And now I am going to try to implement some more logic here so that we can first check whether the number is divisible by other digits which are in the range between 1 to that number or not. So now we will be using this flag way. So here you can see its initial value is 0. Now if number is divisible by i, if number is divisible by i, so I am changing the value of flag as 1. And I am closing this brace. And this for loop is completed here. So after completion of this for loop I am going to check the value of flag. I am going to check the value of flag. So I am writing if flag number equals to 0. So it will print prime as and then we can complete the main function. So now let's iterate again. Number is 7. So first try to divide it by 2. So 7 modulus 2, it is not divisible. So condition will be false. So our control will not go inside if it will directly move to here. So now i is 3. So n is not divisible by 3. Again control will go here directly. Then I will become 4, 5 and 6. So this if condition will not be true in any case if number is 7. It means this if condition will not be true in any case. So we will not go inside this if, right? And if we don't go inside this if, then will flag change its value or not? No. So the value of flag will remain 0 because we are not moving inside if. So here after completion of this loop, you can check if flag is 0, then it will print prime. Right? So with the help of flag, we are going to check whether the number is divisible by any other visit or not. Now take the example of 6. So number is 6. i is 2. 6 is divisible by 2. Right? So it is true. So this time see if it's true. So we are moving inside flag. So it has become 1. It has become 1. Now if i increase i to 3. So 6 is divisible by 3 as well. So it will again move to this flag equals to 1. So value of flag was 1 and it is also 1 now. Then i will become 4. So this time 6 is not divisible by 4. So this condition is false. But still value of flag will remain 1. Because once we are inside this loop and this condition is true. So flag is changed from 0 to 1 and inside this loop, then it will remain 1 always. After completion of this loop, we will check if flag is 0. No, it is 1. So it will print not prime. Right? Now you might be thinking if 6 is divisible by 2. Then we have noticed that if number is divisible by other than 1 and 6 to any number, then it is not prime. So it is not correct if we divide 6 by 3, 4 and 5 as well. So it is not a good practice. So we can terminate our process if number is divisible in any step. So here I am removing this writing in flag equals to 1 and I am using one more keyword. That is break. I am using one more keyword that is break. So what will happen? When 6 is divisible by 2, so flag will be 1 and what break will do? Break will terminate by execution cycle of 4. So when 6 is divided by 2, so this loop will be terminated. Because if condition is 2, flag is equals to 1 and through break we are terminating 1 and control will directly transfer to 2. So now again and again we don't need to assign flag as 1 because once it is true, the same condition is true, then flag will be 1, flag will be 1 and loop will be terminated. So this way I hope you understood how we can calculate whether the enter number is prime or not. So here various combination I explained to you. So this is the final outcome of the program that you can use to check whether a number is prime or not. So use this logic carefully and don't put a printed statement inside of all loop because that will show incorrect results while you will be executing the program. So first identify whether your number is divisible by any other digit except 1 and extra. Then after completion of that process or loop check whether it was divisible or not. So you can use one variable like we use flag. So this way you can identify other process. So I hope you understood whatever I explained in this video. And if you want to watch more programming related videos, you can open my channel and you can go to playlists so there are various programming related videos are available. I have uploaded more than 1000 videos on my channel so you can watch them. So I hope you understood whatever I explained. So thank you for watching this video.