0% found this document useful (0 votes)
6 views4 pages

Experiment No.12 Cs Lab

experimet no12

Uploaded by

Gaurav Ahirwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Experiment No.12 Cs Lab

experimet no12

Uploaded by

Gaurav Ahirwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT 12

OBJECTIVE:-

Write a programm in c to find a key element in an array using linear search

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>

// Function to perform linear search

int linearSearch(int arr[], int size, int key) {

for (int i = 0; i < size; i++) {


if (arr[i] == key) {

return i; // Return the index of the key

return -1; // Return -1 if key is not found

int main() {

printf(“name -gaurav ahirwar rollno.24/B01/062”);

int n, key;

// Input array size

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

// Input array elements

printf("Enter %d elements: ", n);

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

// Input key to search

printf("Enter the key to search: ");


scanf("%d", &key);

// Perform linear search

int result = linearSearch(arr, n, key);

// Output result

if (result != -1) {

printf("Element found at index %d\n", result);

} else {

printf("Element not found in the array\n");

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).

You might also like