Write a C program to perform linear search on integers
In the previous article I have talked about How to take password from user in C language? Today i will cover another very important program that is asked many time in engineering.Yes, you have heard about linear search but have implemented it practically?. Let's start by understanding a C program to perform linear search.
Linear search program in exam usually asked in following ways -
- Write a C program to perform linear search on on integers?
- How to pefrom linear search in C program?
So below is the C program to do linear search.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <stdio.h> #include <conio.h> void main() { int array[10]; int i, N, keynum, found=0; clrscr(); printf ( "Enter the value of N\n" ); scanf ( "%d" ,&N); printf ( "Enter the elements one by one\n" ); for (i=0; i<n ; i++) { scanf ( "%d" ,&array[i]); } printf ( "Input array is\n" ); for (i=0; i<N ; i++) { printf ( "%d\n" ,array[i]); } printf ( "Enter the element to be searched\n" ); scanf ( "%d" , &keynum); /* Linear search begins */ for ( i=0; i < N ; i++) { if ( keynum == array[i] ) { found = 1; break ; } } if ( found == 1) { printf ( "Search operation was successful\n" ); } else { printf ( "Search has failed\n" ); } } |
Output:
Enter the value of N : 4
Enter the elements one by one
3
1
6
2
Input array is 3 1 6 2
Enter element to be searched 6
Search operation is successful
I hope you have enjoy the program. I would like to have feedback from you. Your feedback, question ,queries or comments are always welcome.
Thanks.
Write a C program to perform linear search on integers
Reviewed by CodiBucket
on
10:50
Rating:
No comments: