C program to find Index of an element by element value

Write a C program to search element index from an array by using an index value. In this C programming example, we are going to create a C program to search and find out the element from the array

Algorithm to find Index of an element by element value

  • Include library to take input and give output
  • Take input from loops
  • Checking the condition
  • Print output

C program to find Index of an element by element value

#include<stdio.h>
void main(){
int arr[100],i, a, b;
printf("enter the size of the array:");
scanf("%d",&a);
printf("element are:\n");
for(i=0;i<a;i++){
scanf("%d",&arr[i]);
}
printf("\nenter the element to be searched:");
scanf("%d",&b);
for(i=0;i<a;i++){
if(arr[i]==b){
printf("\nthe element is %d\n",arr[i]);
printf("\npresent at the index %d ",i);
}
}
}

Explanation of the code

Step 1 include the  library

Step 2 declare the main method with void as its return type

Step 3 intialize the array and some required variables

Step 4 print command for the user to give the size of the array

Step 5  using the scanf method to take input

Step 6  now giving a command to the user to give input of elements

Step 7 for loop to take input of array

Step 8 use the scanf method to take input

Step 9 closing the loop of for loop

Step 10 print the command to the user to input the element to be searched

Step 11 take input using the scanf method

Step 12  for loop to search the element

Step 13 checks the condition whether the element enters and the element accessed is the same or not, if same then print the element

Step 14  print the element

Step 15 print the index of the entered value

Closing the scopes

Output

In this article, we have learned how to create a C program to search any element