Experiment No.12 Cs Lab
Experiment No.12 Cs Lab
OBJECTIVE:-
INTRODUCTION:-
This program prompts the user to enter the number of elements in the array, the
elements themselves, and the key to search for. It then performs a linear search
and returns the index of the key if found, or a message indicating that the key is
not present.
ALGORITHM:-
Program:
1.Start
2.Input the number of elements in the array (n).
3.Declare an array of size n.
4.Input n elements into the array.
5. Input the key element to be searched.
6.Set a loop variable i = 0.
7. Repeat steps 8-9 while i < n:
-If arr[i] == key, print "Element found at index i" and exit.
-Increment i.
8.If the key is not found, print "Element not found in the array."
9.End
PROGRAM CODE:-
#include <stdio.h>
int main() {
int n, key;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
// Output result
if (result != -1) {
} else {
return 0;
OUTPUT:-
Upon successful compilation and execution, the program will produce the following output:
RESULT:-
This algorithm follows a sequential search approach where each element is compared one by
one until the key is found or the array ends.
LEARNING FROM EXPERIMENT:-
This experiment gave us an insight into the basic structure of a C program. We learned the
following:
• Linear Search Concept – Checks each element sequentially until the key is
found or the list ends.
• Array Handling – Demonstrates how to take user input and store it in an array.
• Looping & Conditions – Uses a for loop and if condition to search for the key.
• Functions in C – Separates logic using a function for reusability.
• Return Values – Returns the index if found, otherwise -1.
• Time Complexity – Best: O(1), Worst: O(n), Average: O(n).