C Program To Search An Element In An Array

C program to search an element in an array; Through this tutorial, we will learn how to search an element in an array using standard method, function and recursion in c programs.

Programs To Search An Element In An Array

  • C Program To Search An Element In An Array using Standard Method
  • C Program To Search An Element In An Array using Function
  • C Program To Search An Element In An Array using Recursion

C Program To Search An Element In An Array using Standard Method

#include <stdio.h>

int main()
{
    int a[10000],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",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);

    for(i=0; i<n; i++)
    {
        if(a[i]==key)
        {
			printf("element found ");
            return 0;
        }

    }

	printf("element  not  found");
}

The output of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 1 5 4 8 9
Enter the key : 4
element found 

C Program To Search An Element In An Array using Function

#include <stdio.h>

int search(int *a,int n,int key)
{
int i;
    for(i=0; i<n; i++)
    {
        if(a[i]==key)
        {
             return 1;
        }

    }

return 0;

 }



int main()
{
    int a[10000],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",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);

     if(search(a,n,key))
     printf("element found ");
     else
     printf("element not found ");


}

The output of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 4 7 6 9 8
Enter the key : 4
element found 

C Program To Search An Element In An Array using Recursion

#include <stdio.h>

int search(int *a,int i,int n,int key)
{
    if(i<n)
    {
    	if(a[i]==key)
    	return 1;

    	return search(a,i+1,n,key);

	}

return 0;

 }



int main()
{
    int a[10000],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",&a[i]);
    }
     printf("Enter the key : ");
    scanf("%d", &key);

     if(search(a,0,n,key))
     printf("element found ");
     else
     printf("element not found ");


}

The output of the above c program; as follows:

Enter size of the  array : 5
Enter elements in array : 8 7 9 4 5
Enter the key : 7
element found 

Recommended C Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *