Program to Find Value equal to index value

Description

Given an array of n positive integers, to find the elements whose
value is equal to that of its index value follow below program.

C/C++

/* C program to Find Value equal to index value */
//Save it as PrintValueEqualToIndexValue.c

#include<stdio.h>
int main(){

    int i, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the array element : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Entered elements are : ");
    for(i=0;i<n;i++) {
        printf("%d ", arr[i]);
    }

    printf("\nValue equal to index value : ");
    for(i=0;i<n;i++) {
        if(arr[i] == (i+1)) {
            printf("%d ",arr[i]);
        }
    }
    return 0;
}
Input:
Enter the size of array : 
5
Enter the array element : 
12
2
36
6
17

Output:
Entered elements are : 
12 2 36 6 17 
Value equal to index value : 
2

Java

/* Java program to Find Value equal to index value */
//Save it as PrintValueEqualToIndexValue.java

import java.io.*;
import java.util.Scanner;

public class PrintValueEqualToIndexValue {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        System.out.println("Enter the array element : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Entered elements are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
        
        System.out.println("\nValue equal to index value : ");
        for(int i=0;i<n;i++) {
            if(arr[i] == (i+1)) {
                System.out.print(arr[i]+" ");
            }
        }
    }
}
Input:
Enter the size of array : 
5
Enter the array element : 
12
2
36
6
17

Output:
Entered elements are : 
12 2 36 6 17 
Value equal to index value : 
2

Related Programs

1) write a Program to find index of two array elements whose sum is equal to given value
2) Program to make pair of elements alternatively
3) Program to Print How Many Numbers Smaller than Current Number
4) Remove Duplicate Elements From Array
5) Insert a number in an array sorted in ascending order
6) Program to merge two sorted arrays
7) Program to read and display 2D array
8) Program to Rearrange an array such that arr[i]=i
9) Program to Find missing odd number in first n odd number
10) Program to Check Array is Perfect or Not
Share Me

Leave a Reply