Home / Programs / Write a program that will search and find out the position where the given key element exist in a user chosen array and print it as output.
Programming Example

Write a program that will search and find out the position where the given key element exist in a user chosen array and print it as output.

👁 1,080 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that will search and find out the position where the given key element exist in a user chosen array and print it as output.

Program Code

#include <stdio.h>
#include <stdlib.h>

int main()
{

  int a[30],n,i,key, FOUND=0;

  printf("\n How many numbers ");
  scanf("%d",&n);

  if(n>30)
  {
    printf("\n Too many Numbers");
    exit(0);
   }

  printf("\n Enter the array elements \n");

  for(i=0 ; i<n; i++)
    scanf("%d", &a[i]);

  printf("\n Enter the key to be searched \n");
  scanf("%d",&key);

  for(i=0 ; i<n; i++)
    if(a[i] == key)
    {
      printf("\n Found at %d",i);
      FOUND=1;
     }
  if(FOUND == 0)
    printf("\n NOT FOUND...");

  return 0;
 }

Output


 How many numbers 5

 Enter the array elements
5
4
3
2
1

 Enter the key to be searched
3

 Found at 2

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.