Python Program for Linear Search using For loop. In linear search algorithm we match the element to be searched with the entire list of elements. Therefore its a brute force approach.
Copy the below python program and execute it to see the output. At the end of example, I have shared the real time program output.
1 2 3 4 5 6 7 8 9 | n=int(input("Enter the number to be searched (1-10):")) a=[1, 2, 4, 3,5,7,9,8,6,10 ] for i in range(1,(len(a))): if n==a[i]: print("number found at",i+1) |
Output
1 2 3 | Enter the number to be searched (1-10):7 Number found at 6 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.