0% found this document useful (0 votes)
7 views1 page

01.binary Search Program For C Program in C

The document contains a C program that demonstrates the concept of binary search. It prompts the user to input a number of positive integers, stores them in an array, and allows the user to search for a specific integer within that array using the binary search algorithm. The program outputs the position of the searched integer if found, or indicates that it is not found.

Uploaded by

Shivam chavan
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)
7 views1 page

01.binary Search Program For C Program in C

The document contains a C program that demonstrates the concept of binary search. It prompts the user to input a number of positive integers, stores them in an array, and allows the user to search for a specific integer within that array using the binary search algorithm. The program outputs the position of the searched integer if found, or indicates that it is not found.

Uploaded by

Shivam chavan
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/ 1

22/05/2023, 22:04 127.0.0.1:5500/01.binary_search.

// 01.Programm to demonstrate the concept of Binary search

#include <stdio.h>
int main()
{
printf("\nHow many positive integers you want to enter :");
int n;
scanf("%d", &n);

int a[n];

printf("Enter %d positive integers.\n", n);


int i;
for (i = 0; i < n; i++)
scanf("%d", &a[i]);

printf("\nThe Entered array is :");


for (i = 0; i < n; i++)
printf("%d ", a[i]);

printf("\n\nEnter the element you want to search :");


int search;
scanf("%d", &search);

int left_index, right_index, mid_index;


left_index = 0;
right_index = n - 1;
mid_index = (left_index + right_index) / 2;
while (left_index <= right_index)
{
if (search == a[mid_index])
break;

else if (search < a[mid_index])


right_index = mid_index - 1;

else
left_index = mid_index + 1;

mid_index = (left_index + right_index) / 2;


}

if (left_index > right_index)


printf("%d is not found.", search);
else
printf("%d is found at %d position\n", search, mid_index + 1);
return 0;
}

/*
OUTPUT:
How many positive integers you want to enter :10
Enter 10 positive integers.
5 9 17 23 25 45 59 63 71 89

The Entered array is :5 9 17 23 25 45 59 63 71 89

Enter the element you want to search :25


25 is found at 5 position
*/

127.0.0.1:5500/01.binary_search.c 1/1

You might also like