Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
24. Write a program to search an element in an array using Linear Search.
/*
File: Prgrm24.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to find the minimum and maximum elements of the array.
*/
#include <stdio.h>
int main (void)
{
int array [10];
int key;
int i;
//Input elements of array
for (i = 0; i < 10; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &array [i]);
}
//Input key element
printf ("\nInput key: ");
scanf ("%d", &key);
//Finding the key element
for (i = 0; i < 10; i++)
{
if (key == array [i])
break;
}
//Output the result
if (i == 10)
printf ("\n%d is not present in the array.", key);
else
printf ("%d is found in array at %d position.", key, i + 1);
return 0;
};
Output
Input 1 element: 27
Input 2 element: 46
Input 3 element: 92
Input 4 element: 42
Input 5 element: 80
Input 6 element: 58
Input 7 element: 13
Input 8 element: 22
Input 9 element: 73
Input 10 element: 38
Input key: 80
80 is found in array at 5 position.
Input 2 element: 46
Input 3 element: 92
Input 4 element: 42
Input 5 element: 80
Input 6 element: 58
Input 7 element: 13
Input 8 element: 22
Input 9 element: 73
Input 10 element: 38
Input key: 80
80 is found in array at 5 position.
No comments:
Post a Comment
Please do not post spam links.