C C++ program for searching in array-Linear Sequential search

C C++ program for searching in array-Linear Sequential search

If statement और decision making statements के बारे में अधिक जानने के लिए देखें— If statements in C

Switch statement और multibranching statements के बारे में अधिक जानने के लिए देखें—Switch statements in C

C Language में Array और इसके विभिन्न प्रकारो के बारे में और अधिक जानने के लिए देखें— Arrays in C

C program for searching in array-Linear Sequential search

#include<stdio.h>
int main()
{
    int a[5],i,key,flag=1;
    printf("Enter values of array: ");
    for(i=0;i<5;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter search key: ");
    scanf("%d",&key);
    for(i=0;i<5;i++)
    {
        if(a[i]==key)
        {
            printf("Search key is present in position %d",i+1);
            flag=0;
        }
    }
    if(flag==1)
    {
        printf("Search key is not present");
    }
    return 0;
}

C++ program for searching in array-Linear Sequential search

#include<iostream>
using namespace std;
int main()
{
    int a[5],i,key,flag=1;
    cout<<"Enter values of array: ";
    for(i=0;i<5;i++)
    {
        cin>>a[i];
    }
    cout<<"Enter search key: ";
    cin>>key;
    for(i=0;i<5;i++)
    {
        if(a[i]==key)
        {
            cout<<"Search key is present in position "<<i+1;
            flag=0;
        }
    }
    if(flag==1)
    {
        cout<<"Search key is not present";
    }
    return 0;
}

Output of C C++ program for searching in array-Linear Sequential search

C C++ program for searching in array-Linear Sequential search

Related array example programs in C C++

  1. Declaration and initialization of array
  2. Input-output in array
  3. Sum and average of array elements
  4. Largest Element in Array
  5. Smallest element in Array
  6. Reverse Elements of Array
  7. Merging two arrays
  8. Sorting in array (Selection sort)
  9. Pointer to array
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer