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

Coding in C

#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;
}

Coding in C++

#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

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

Published by

admin

I am a computer teacher, programmer and web developer