Home / Programs / C Program to Search an element in Array
Programming Example

C Program to Search an element in Array

👁 1,138 Views
💻 Practical Program
📘 Step by Step Learning
C Program to Search an element in Array

Program Code

#include<stdio.h>
 
int main() {
   int a[30], ele, num, i;
 
   printf("\nEnter no of elements :");
   scanf("%d", &num);
 
   printf("\nEnter the values :");
   for (i = 0; i < num; i++) {
      scanf("%d", &a[i]);
   }
 
   //Read the element to be searched
   printf("\nEnter the elements to be searched :");
   scanf("%d", &ele);
 
   //Search starts from the zeroth location
   i = 0;
   while (i < num && ele != a[i]) {
      i++;
   }
 
   //If i < num then Match found
   if (i < num) {
      printf("Number found at the location = %d", i + 1);
   } else {
      printf("Number not found");
   }
 
   return (0);
}

Output

Enter no of elements : 5
11 22 33 44 55
Enter the elements to be searched : 44
Number found at the location = 4

Explanation

none

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.