20_10

Write a C program to find the first repeated element in an array.

Source code:

 //Write a C program to find the first repeated element in an array of size 5

#include <stdio.h>

int main()

{

    int arr[5];

    int i,j,n=5;

    int ind=-1,ele; /* to store index & element */

    /* read array elements */

    for(i=0; i<n; i++)

    {

    printf("Enter the value for %d: ",i);

        scanf("%d",&arr[i]);

    }

for(i=0; i<n; i++)

    {

        for(j=i+1; j<n; j++)

        {

            if(arr[i]==arr[j])

            {

                ele=arr[j];

                ind=j;

                goto l;

                break;

            }

        }

    }

l: if(ind!=-1)

printf("%d repeated at index %d\n",ele,ind);

    else

        printf("There is no repeated element\n");

return 0;

}

Output:
C program to find the first repeated element in an array


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...