C program to search an element in an array
In this post, you will learn how to write C programs to search for an element in an array. There are many searching algorithms available to search an element in an array. Here, we have mentioned different approaches to achieving this task.
Simple C program to search an element in an array
In the given C program, we use the standard method to search for an element in an array. First, it asks the user to enter the size of an array and store it in a variable n, and then it asks again to enter the elements of an array and store them in an array arr. Next, it asks the user to provide the element that they want to search for in an array and store in a variable named "ele". Within the for loop, it compares the element with each element of the array as arr[i]==ele and prints the element found.
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[10],i,n,ele;
printf("Enter array size: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &ele);
for(i=0; i<n; i++)
{
if(arr[i]==ele)
{
printf("%d found at position %d", ele, i+1);
return 0;
}
}
printf("Element not found");
}
Output of the above code:
Enter array size: 5
Enter array elements: 34 66 22 90 89
Enter element to search: 66
66 found at position 2
C program to search an element in an array using function
In the given C program, we define a search_element() function that compares each element of an array with the key we want to search for using the if condition and the for loop. If any element matches with that entered element, then the function returns 1, otherwise, it returns 0 to the main() function.
#include <stdio.h>
#include <conio.h>
int search_element(int *arr,int n,int ele)
{
int i;
for(i=0; i<n; i++)
{
if(arr[i]==ele)
{
return 1;
}
}
return 0;
}
int main()
{
int arr[10],i,n,key;
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter elements in array: ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter the key: ");
scanf("%d", &key);
if(search_element(arr,n,key))
printf("Element found.");
else
printf("Element not found.");
}
Output of the above code:
Enter size of the array: 6
Enter elements in array: 90 83 78 25 82 75
Enter the key: 25
Element found.
C program to search an element in an array using recursion
Recursion function is a function that is called itself. In the given C program, we use the recursion function to search for an element in an array. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.
#include <stdio.h>
#include <conio.h>
int search(int *arr,int i,int n,int ele)
{
if(i<n)
{
if(arr[i]==ele)
return 1;
return search(arr,i+1,n,ele);
}
return 0;
}
int main()
{
int arr[10],i,n,ele;
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter elements in array: ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &ele);
if(search(arr,0,n,ele))
printf("Element found.");
else
printf("Element not found.");
}
Output of the above code:
Enter size of the array: 7
Enter elements in array: 89 73 28 74 23 60 51
Enter element to search: 74
Element found.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative