Find the index of an element in an array

Problem statement:- Program to Find the index of an element in an array.

Example:-

              Input : Given 

                                size=5

                                 arr[]=[4,5,7,3,9]  

                                Search Element=7

                               /*Index[0]=4

                                Index[1]=5

                                Index[2]=7

                                Index[3]=3

                                 Index[4]=9*/

                Output:

                        Index=2              

Data requirement:-

   Input Data:- size, arr, ele

  Output Data:-ele, index

  Additional Data:- i, temp

Program in C

Here is the source code of the C Program to Find the index of an element in an array.

Code:

#include<stdio.h>
main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size];
    int i,temp=0;
    printf("Enter the Element of the array:\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }

    int ele;
    printf("\nEnter the search element:");
    scanf("%d",&ele);
    printf("\nArray elements are:");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    int index;
    for(i=0;i<size;i++)
    {
        if(arr[i]==ele)
        {
            temp=1;
            index=i;
        }
    }
    if(temp==1)
        printf("\nIndex of Search Element %d is %d",ele,index);
    else
        printf("\nElement is not found....");
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
5
7
3
9

Enter the search element:7

Array elements are:4 5 7 3 9
Index of Search Element 7 is 2

Program in C++

Here is the source code of the C++ Program to Find the index of an element in an array.

Code:

#include<iostream>
using namespace std;
main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size];
    int i,temp=0;
    cout<<"Enter the Element of the array:\n";
    for(i=0;i<size;i++)
    {
        cin>>arr[i];
    }

    int ele;
    cout<<"\nEnter the search element:";
    cin>>ele;
    cout<<"\nArray elements are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<" ";
    }
    int index;
    for(i=0;i<size;i++)
    {
        if(arr[i]==ele)
        {
            temp=1;
            index=i;
        }
    }
    if(temp==1)
        cout<<"\nIndex of Search Element "<<ele<<" is "<<index;
    else
        cout<<"\nElement not found....";
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
4
3
9
7
5
10

Enter the search element:10

Array elements are:4 3 9 7 5 10
Index of Search Element 10 is 5

Program in Java

Here is the source code of the Java Program to Find the index of an element in an array.

Code:

import java.util.Scanner;
public class SearchElement {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
  int i,temp=0,size;
     size=sc.nextInt();
     int arr[ ]=new int[size];
  System.out.println("Enter the Element of the array:");
      for(i=0;i<size;i++)
     {
         arr[i]=sc.nextInt();
     }
      int ele;
      System.out.print("\nEnter the search element:");
      ele=sc.nextInt();
      
      System.out.print("\nArray elements are:");
      for(i=0;i<size;i++)
      {
       System.out.print(arr[i]+" ");
      }
      int index = 0;
      for(i=0;i<size;i++)
      {
          if(arr[i]==ele)
          {
              temp=1;
              index=i;
          }
      }
      if(temp==1)
      System.out.print("\nIndex of Search Element "+ele+" is "+index);
      else
      System.out.print("\nElement is not found....");
     sc.close();
}
}

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
3
12
14
15

Enter the search element:14

Array elements are:3 12 14 15 
Index of Search Element 14 is 2

Program in Python

Here is the source code of the Python Program to Find the index of an element in an array.

Code:

arr=[]
temp=0
pos=0
index=0
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
print("Enter the search element:")
ele=int(input())
print("Array elements are:")
for i in range(0,size):
    print(arr[i],end=" ")
for i in range(0,size):
    if arr[i] == ele:
            temp = 1
            index=i
if temp==1:
    print("\nIndex of Search Element ",ele," is ",index)
else:
    print("\nElement not found....")

Input/Output:
Enter the size of the array: 3
Enter the Element of the array:
4
5
2
Enter the search element:
5
Array elements are:
4 5 2 
Index of Search Element  5  is  1

Post a Comment

0 Comments