Write a C program that uses non recursive function to search for a Key value in a given list of integers using Linear search.

Algorithm:

Step 1: Start
Step 2: Read n, a[i], key values as integers
Step 3: Search the list
         While (a[i] != key && i <= n)
          i = i + 1
           Repeat step 3
Step 4: Successful search
         if(i = n + 1) then
          print “Element not found in the list”
          Return(0)
         else
          print “Element found in the list”
          Return (i)
Step 6: Stop

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
 int i, a[20], n, key, flag = 0;
 clrscr();
 printf(“Enter the size of an array \n”);
 scanf(“%d”, &n);
 printf(“Enter the array elements”);
 for(i = 0; i < n; i++)
 {
  scanf(“%d”, &a[i]);
 }
 printf(“Enter the key elements”);
 scanf(“%d”, &key);
 for(i = 0; i < n; i++)
 {
  if(a[i] == key)
  {
   flag = 1;
   break;
  }
 }
 if(flag == 1)
  printf(“The key elements is found at location %d”, i + 1);
 else
  printf(“The key element is not found in the array”);
 getch();
}

Input & Output:

Enter the size of an array 6
Enter the array elements 50 10 5 200 20 1
Enter the key element 1
The key Element is found at location 6