Skip to content
Home » C program to find index of given number in an Array

C program to find index of given number in an Array

Below is the program to find the index of given number in an array.

#include <stdio.h>
int main(){
    int i,v;
    printf("Enter no.of values u want to enter:\n");
    scanf("%d",&i);
    int arr[i];
    printf("Enter set of nums:\n");
    for(v=0;v<i;++v){
        scanf("%d",&arr[v]);
    }
    int s;
    printf("Enter element u want to search for:\n");
    scanf("%d",&s);
    int y,n=0;
    for(y=0;y<i;++y){
        if(arr[y]==s){
            printf("The index of the given numbers is %d",y);
            n=1;
            break;
        }
    
    
    }
    if(n==0){
            printf("The number is not present in the array");
    }      
    return 0;
}

Output 1:

Enter no.of values u want to enter:
5
Enter set of nums:
3
6
7
2
6
Enter element u want to search for:
6
The index of the given numbers is 1

Output 2:

Enter no.of values u want to enter:
4
Enter set of nums:
3
6
7
8
Enter element u want to search for:
5
The number is not present in the array