Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions C/Program-52/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Program 52

Write a program to find whether the given element is present in the array or not.

39 changes: 39 additions & 0 deletions C/Program-52/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>

int main() {
int n;//number of elements of array;
printf("Enter the number of elements");
scanf("%d",&n);
int arr[n];
printf("\nEnter the elements");
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int x;
printf("\nEnter the key to search");
scanf("%d",&x);
int l=0,r=n-1,m=0;
int f=1;
while (l <= r)
{
m = l + (r - l) / 2;

// Check if x is present at mid
if (arr[m] == x)
{
printf("\nElement found");
f=0;
break;
}

// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;

// If x is smaller, ignore right half
else
r = m - 1;
}
if(f==1)
printf("\nElement not found");
return 0;
}