Search Element in Array in C Language

Spread the love
search-element-in-array-in-c-language

Program Description:

Let’s write a program to Search Element in Array in C programming language. The Program should ask for the number to search and then search for the given number in the input array. Here is the example input and output of the program.

Excepted Input and Output:

Input:

Output:

FOUND - The 23 number Found at index 4

Prerequisites:

It is good to know the basics of the Arrays and Functions in C language. Please go through the following articles to learn more about these topics.

Search Element in Array in C Program Explanation:

  1. Start the program by declaring the values array with the max size of 100. You can also use a Macro or Const global variable to hold the 100 value. But we are directly using here.
  2. Also declare the supporting variables targetNum, i, and size
  3. Take the desired array size from the user and update the size variable.
  4. Now, Prompt the user to provide the array elements. Use a For Loop to iterate over the array elements and update them.
    • Use a variable i to traverse from i=0 to i=size-1.
    • At each iteration, Prompt the user for input and update the values[i] element (Use scanf() function to read the elements)
  5. Take the targetNum from the user, The targetNum is the number to be searched in the values array.
  6. To search for the Number in Array
    • Iterate from zero(0) to size-1 and compare the each element value with the targetNum value.
    • if the values[i] == targetNum condition is true, Then we found the targetNum in the array.
    • Display the FOUND message on the console and stop the program using the return statement
  7. If the above loop is completed successfully, Then we haven’t found the targetNum in the values array. So display the NOT FOUND message on the standard output.

📢 This Program uses Linear Search and the Time Complexity of the Program is Big O(n). Where n is the size of the array.

Algorithm of Search Element in Array in C:

  1. Start the program
  2. declare values[100] array and targetNum, i, and size variables.
  3. Take array size from the user
  4. Update the array elements with user-provided numbers
    • Iterate from Zero(0) to size-1 using for loop
    • update the values[i] element
  5. Prompt the user for number to search and store in targetNum
  6. Search for the targetNum in the values array.
    • Traverse the array from Zero(0) to size-1 using for loop
    • If the (values[i] == targetNum) is true, Then we found the targetNum. Display the FOUND message and terminate the program.
  7. If the loop is completed, Then the targetNum is NOT FOUND in the values array. Display the NOT FOUND message.
  8. stop the program

Program to Search Element in Array in C:

Here is the C Program to search for a number in the array.

Program Output:

Let’s compile and run the program. We are using the GCC compiler in the Linux operating system.

Compiling the C program using GCC.

gcc search-element.c

Run the program.

search-for-a-element-in-array-in-c-program-output

Related Array Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply