
C program to implement linear search in an array
Algorithm
Input: Input size of the array, elements of the array and element to be searched
output: Searches for a user input element in the array and displays its index if any
- Start
- Input the size of the array say n and enter the elements of the array a[n]
- Input the element to be searched
- i = 0
- found = 0
- while ( i < n )
5.1 if a [ i ] = = search
5.1.1 print "Found the required element at index " i
5.1.2 found = 1
5.2 i++ - if ( found = = 0)
7.1 print "The element not found in the array" - stop
C Program
#include <stdio.h>
int main()
{
int n, found = 0, search;
printf("Enter the size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements one by one\n");
for (int i = 0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter the element to be searched:");
scanf("%d", &search);
for (int i = 0; i < n; i++) {
if (a[i] == search) {
printf("Found the required element at index %d\n", i);
found = 1;
} }
if (found == 0) {
printf("The element %d not found in the array", search);
}
}
Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here
Flowchart

Note: This Flowchart was created using a program called Raptor. The flowchart file can be downloaded by clicking here
Comments