/* SEARCH ELEMENT AND PRINT THE INDEX OF ELEMENT*/
#include<stdio.h>
int main()
{
int list[100],n,i,x,p;
printf("enter the size of list");
scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
p=-0;
printf("enter the element to be search");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(list[i]==x)
p=i;
}
if(p>=0)
printf("index of element %d is %d",x,p);
else
printf("try again");
getch();
return 0;
}
OUTPUT FINAL SUMITION
#include<stdio.h>
int main()
{
int list[100],n,i,x,p;
printf("enter the size of list");
scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
p=-0;
printf("enter the element to be search");
scanf("%d",&x);
for(i=0;i<n;i++)
{
if(list[i]==x)
p=i;
}
if(p>=0)
printf("index of element %d is %d",x,p);
else
printf("try again");
getch();
return 0;
}
OUTPUT FINAL SUMITION

p=i+1;
ReplyDeletei am seeing everyone's output are same those
ReplyDeletewho are uploaded the image of output ...
Buddy everyone has the same problem....:p
DeleteThere is no logic to initialize the variable 'p' with negative '0'. If the elements are in duplicacy manner in the array then your code give the last duplicate element's index value. Take an example of 5 array size and insert elements {2,2,2,2,2}. Your code will give position of 2 is '4'.
ReplyDeletekoi ni
Deleteall are coping n pasting........
ReplyDelete