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;
}
No comments:
Post a Comment