Binary Search - C Programming

/* Program to find the specified element from array using Binary search */
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100],i,no,l,h,m,t,f=1; /* Local definitions */
/* Statements */
clrscr();
printf("Enter the size of the array \n");
scanf("%d", &no);
printf("Enter the elements in ascending order");
for(i=0;i<no;i++)
scanf("%d", &a[i]);
printf("Enter the elements to be searched");
scanf("%d", &t);
l=0;
h=no‒1;
while(l<=h)
{
m=(1+h)/2;
if(t<a[m])
h=m‒1;
else if(t>a[m])
l=m+1;
else
{
printf("\n Entered %d is in position %d",t,m+1);
f=2;
break;
}/* if else */
} /* while */
if(f==1)
printf("\n Entered element is not in the array");
getch();
} /* main */
OUTPUT
Enter the size of the array 5
Enter the elements in ascending order 1 3 4 5 6
Enter the elements to be seareched 3
Entered 3 is in position 2
EXPLANATION: The above program gets the last number no from the terminal then reads the ascending order number through for loop. The while loop statement executes the block of statement then prints the position of the entered number as output. This program refers the binary searching.
Computer Programming C Laboratory: String C Programs : Tag: C Programming : - C Program to find the specified element from array using Binary search
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation