0% found this document useful (0 votes)
3 views3 pages

Linear and Binary Search in C

The document contains two C programs for searching integers in an array: a Linear Search and a Binary Search. The Linear Search program iterates through the array to find a specified number, while the Binary Search program requires a sorted array and uses a divide-and-conquer approach to locate the number. Both programs prompt the user for input and display the result of the search.

Uploaded by

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

Linear and Binary Search in C

The document contains two C programs for searching integers in an array: a Linear Search and a Binary Search. The Linear Search program iterates through the array to find a specified number, while the Binary Search program requires a sorted array and uses a divide-and-conquer approach to locate the number. Both programs prompt the user for input and display the result of the search.

Uploaded by

Sujal Rajvansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Linear Search

#include <stdio.h>

#include <stdlib.h>

int main()

int array[50], search, c, n;

printf("enter number of elements in array\n");

scanf("%d", &n);

printf("enter %d integers\n", n);

for(c=0;c<n;c++)

scanf("%d", &array[c]);

printf("enter a number to search\n");

scanf("%d", &search);

for(c=0; c<n; c++)

if(array[c]== search)

printf("%d is present at location %d.\n", search, c+1);

break;

if(c==n)

printf("%d is not present in the array. \n", search);


return 0;

Binary Search

#include <stdio.h>

#include <stdlib.h>

int main()

int c, first, last, middle,n,search, array[100];

printf("enter number of elements in array\n");

scanf("%d", &n);

printf("enter %d integers\n", n);

for(c=0;c<n;c++)

scanf("%d", &array[c]);

printf("enter a number to search\n");

scanf("%d", &search);

first=0;

last=n-1;

middle =(first+last)/2;

while(first <= last)

if(array[middle]< search)

first = middle+1;

else if(array[middle]== search)


{

printf("%d found at location %d \n", search, middle+1);

break;

else

last = middle-1;

middle =(first +last)/2;

if(first > last)

printf("not found %d in the list.\n", search);

return 0;

You might also like