Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Linear search in a sorted array

Posted By: Andrew Brown     Category: C Programming     Views: 6020

Linear search in a sorted array.

Code for Linear search in a sorted array in C Programming

#include <stdio.h>
#include <conio.h>

void main( )
{
    int arr[10] = { 1, 2, 3, 9, 11, 13, 17, 25, 57, 90 } ;
    int i, num ;

    clrscr( ) ;

    printf ( "Enter number to search: " ) ;
    scanf ( "%d", &num ) ;

    for ( i = 0 ; i <= 9 ; i++ )
    {
        if ( arr[9] < num || arr[i] >= num )
        {
            if ( arr[i] == num )
                printf ( "The number is at position %d in the array.", i ) ;
            else
                printf ( "Number is not present in the array." ) ;
            break ;
        }
    }

    getch( ) ;
}
  
Share: 

 
 

Didn't find what you were looking for? Find more on Linear search in a sorted array Or get search suggestion and latest updates.

Andrew Brown
Andrew Brown author of Linear search in a sorted array is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!