 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can perform linear search in C program. So before starting explanation, I just want to give you one information. If you go to detail or description of this video, you will find links of various players. So you can watch those programs if you want to learn programming. Now I'm going to explain how we can perform linear search with the help of C program. So first I'm going to read an array and then I'm going to read a number that we want to search in that particular array. So let me draw one array here. So let's say this is one the array. These are indexes and its name is A and these are in this right and these are the values which are assigned into this. So first we are going to read this array from the code and then we are going to read a number. Let's say I want to search 15. So I need to identify whether 15 is available in this one the array or not, or we can also identify on which location it is. And let's say first we will check it for 15 and then we will check it for 25. So if n is 25, so you can see 25 is not available in this array. So in that case, the program should print the number is not found. So the program should be printing both the results. If it is available, then it will say number is present at this location. And if the number is not available, then it should say number is not present, right? So now I'm going to implement the solution. So first of all, I'm going to take an array, then the number which we want to search then variable I and I'm using a flag variable that is at whose initial value is 0. Then after this, I'm going to write printf, which will display a message, enter 10 numbers, then with the help of for loop, I'm going to read all those numbers. So for loop will repeat this kind of and all the numbers will be stored inside this. Now after this, I'm going to write one more printf and it will display the message enter the number to be searched. So user has to enter the number that he want to search in the array and then to receive that as input, I'm writing this. So this part will read any and this part will read the number that you want to search, whether it is present inside array or not. Right. So this is quite easy. So I hope you understood till now, whatever is implemented. So now I'm going to write the code that will perform linear search. So I hope you also understood why it is called linear search because we are searching from the first number and we are going to search that number till end. So linearly we are going to search that number one by one if it is available and if it is available on a particular position, then we need to break the rotation of loop or you can say we need to break the searching process so that unnecessarily we don't match the number which we are going to search with other numbers. Right. So after this, I'm going to write one more loop. So this will also repeat 10 times because we don't know that a number will be available. But if inside this loop, any time number is found, we can break the loop with the help of breakkeeper. So now with this loop, first I'm going to write if condition. So if A of 5 W equals to N, so A of 5 means element of array and N is the variable which is having the number which we want to search. Right. So in first row, in first row, wherever anyone implemented this solution, so there may be thinking like if it is true, then we can print number is found and if it is false, then we can print number is not found. Right. So in first row, you might be thinking like we can implement the solution like this, but it is incorrect. So I'm repeating it is incorrect. So now I'm telling you why it is incorrect. So let's say number is 15. I'm going to search 15 and these are the elements of array. Right. So loop will start from 0. So first it will check a 0. So a 0 location is 11. So first it will check 11 W equals to 15. So this condition is false. So it will print not found. Right. Then again, I will be incremented. I will become one. So A of 1 position is 11 12. So 12 is again not equals to 15. Then again it will print not found. So for 11, 12, 13, 14, it will print not found. Then in case of 15, it will print found. And for again five numbers, it will print not found. So it means this process is incorrect. So I am removing these statements. So I hope you understood inside this loop, inside this loop, we cannot print whether number is present or not. We can just identify whether it is present or not. So now I am going to use this variable inside this loop. So if this condition is true, then I am changing the value of flag. So initially it was 0. If this condition is true, then I am changing its value with 1. And if this condition is true, it means number is present. So we can break the loop. So we can break the loop and I am closing the loop. So you can see I have not mentioned anywhere inside this loop with the help of printf like number is found or not. I just changed the value of f variable with 1. Initially it was 0. So now again I am going to pass this with same number like number is 15 and array is continuing to be present. So first time when a0 will be checked, so a0 is having 11, 11 equals to 15. So condition is false. So we are having only two parts for it. We are not having any other part. So if this condition is false, then control will be transferred to i-pressure. So i will be 1. So next time a1 that is 12. So 12 will be compared with 15. So 12 and 15 both are unequal. So again it will be false. So i will be implemented. So this way 13 will be false. 14 will be false. Then 15 comes. So 15 a0 is having 15 and is having 15. So both are true. In that case f will become 1 and break will terminate this loop. So after execution of this break, this loop will not work and control will be transferred here just after the loop. It means if this condition is true, then f will be having 1. And if f is having 1, it means number is found. So after this loop you just need to implement 1 if and you need to check what is the value of f. So let's say if fw equals to 1, if fw equals to 1 or you can write simply f inside if you can write fw equals to 1 or you can simply write f because f is containing 1. So any nonzero value in programming is considered as true. So here you can write printf number is number is found and else you can print number is not found and you can close your main function. So now you might be thinking when value of f will become 0. So if we take 25 as value of n, so we want to search 25 inside this edit. So you can see 25 is not present. It means this if condition will not become true anytime. So this loop will repeat 10 times and all 10 times this if will be false. So if this condition is false, then f will not become 1 anytime. So all 10 rotations, if we are performing all 10 rotations, this if condition is false. So what will be the value of f? 1 or 0? It will be 0 because if it is not true, so if this condition is not true, it means we are not changing the value of f. So it will remain 0 and after completion of this loop, if f is not equal to 1, it means it is 0, so it will print number is not. So if f is 1, it means number is available, otherwise number is not available. So this way with this condition we are able to identify whether a number is found inside array or not. Now one more thing is remaining. So it is printing number is found but it is not printing at which location. It is not printing at which location. So now what you can do, let's say number is 15. So 15 is present at this location and at that time value of i was 4. A4, A4 location, 4th index is having 15 and 15 was equals to 15. So f was 1 and loop terminated using break. So 1, 2, 3, 4, 5. So this location is 5th but its index is 4 and loop is terminated. So last value of i was 4 when number was found. So here you can print, here you can print number is found at percentage location and then you can print i plus 1. So i was 4, so we are printing 4 plus 1. So the output will be number is found at 5th location. Number is found at 5th location. So this way you can add like at which location number is available with the help of i. So this way the program that I explained to implement linear search so that we can find better of particular number is available in an array or not. And one more thing you need to notice let's say 15 is available one more time in this array. So in this program we are only finding the first occurrence of that number. So 15 is available at this position it means it is found. So no need to check any other occurrence of this number. So if array is containing all elements as 15 let's say all elements are 15. So if at first position 15 is available so no need to check other elements whether they are 15 or not. So this way in case of linear search you just need to find out whether that number is available at initial positions or not. If it is available then no need to search for another position and if it is not available then in that case you need to search for all the elements. Because maybe it is available at last location so you need to check all the elements in the loop rotation. And I also explained the use of way so I hope you understood why we implemented break here so that we can terminate the process. So I hope you understood whatever I explained in this video. Thank you for watching this video.