#include<stdio.h>
#include<conio.h>
int main()
{
int list[100], n,i,x,p;
printf("Enter the size of the list=");
scanf("%d", &n);
printf("Enter the elements=");
for(i=0;i<n;i++)
scanf("%d", &list[i]);
p=0;
printf("Enter the elements to be 'searched':");
scanf("%d", &x);
for(i=0;i<n;i++)
{
if(list[i]==x)
p=i;
}
if(p>=0)
printf("'Position of element %d is %d", x,p);
else
printf("Try Again");
getch();
}
Output:
Enter the size of the list=5
Enter the elements=1
2
3
8
7
Enter the elements to be 'searched':8
'Position of element 8 is 3
#include<conio.h>
int main()
{
int list[100], n,i,x,p;
printf("Enter the size of the list=");
scanf("%d", &n);
printf("Enter the elements=");
for(i=0;i<n;i++)
scanf("%d", &list[i]);
p=0;
printf("Enter the elements to be 'searched':");
scanf("%d", &x);
for(i=0;i<n;i++)
{
if(list[i]==x)
p=i;
}
if(p>=0)
printf("'Position of element %d is %d", x,p);
else
printf("Try Again");
getch();
}
Output:
Enter the size of the list=5
Enter the elements=1
2
3
8
7
Enter the elements to be 'searched':8
'Position of element 8 is 3
the 1st element's position is not shown....please check your program
ReplyDelete#include
ReplyDelete#include
int main()
{
int arr[100], num,i,n,found=0,pos=-1;
printf("Enter the number of elements in the array=");
scanf("%d", &n);
printf("Enter the elements=");
for(i=0;i<n;i++)
{
printf("\n arr[%d]=",i);
scanf("%d", &arr[i]);
}
printf("\n Enter the element to be searched=");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
found=1;
pos=i;
printf("\n %d is found in the array at postion=%d",num,i);
break;
}
}
if(found==0)
printf("\n %d does not exist in the array...",num);
getch();
return 0;
}
here your first element position is not shown
ReplyDeleteplease check the other program that i have uploaded...
ReplyDelete