write a program to search for an element in a given array.
If the array was found then display its position otherwise
display appropriate message in c language
Answers were Sorted based on User's Feedback
Answer / anuja kulkarni
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int ele,temp=0,pos=0;
clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);
// searching for the element
for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
pos=i;
}
}
if (temp==1)
printf("Element found %d , position==%d",ele,pos);
else
printf("Element not found\n");
} // end of main()
Is This Answer Correct ? | 483 Yes | 132 No |
Answer / sharan
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int ele,temp=0;
clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);
// searching for the element
// searching for the element
for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
printf("Element found %d , position=%d\n",ele,i);
}
}
if (!temp)
printf("Element not found\n");
} // end of main()
Is This Answer Correct ? | 149 Yes | 97 No |
Answer / bishwas bidari
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int ele,temp=0,pos=0;
clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be search\n");
scanf("%d",&ele);
/* searching for the element*/
for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
pos=i;
}
}
if (temp==1)
printf("Element found %d , position==%d",ele,pos);
else
printf("Element not found\n");
}
Is This Answer Correct ? | 85 Yes | 52 No |
Answer / poorni
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,temp=0,n,pos=0,element;
//clrscr();
printf("Search an Element in Array\n");
printf("Enter the number of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the value for elements: ");
scanf("%d",&a[i]);
}
printf("Enter the searching element: ");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
{
temp=1;
pos=i;
}
}
if (temp==1)
printf("Element found %d , position=%
d",element,pos);
else
printf("Element not found\n");
getch();
}
Is This Answer Correct ? | 56 Yes | 31 No |
Answer / kapil hansrajani
include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,n;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the element to search : ");
scanf("%d",&j);
for(i=0;i<n;i++)
{
if(a[i]==j)
{
printf("The position of the element %d is %d ",j,i);
break;
}
}
getch();
}
Is This Answer Correct ? | 48 Yes | 26 No |
Answer / kirlosky
why should we mention temp=something ..... wat is temp here
plz explain
Is This Answer Correct ? | 70 Yes | 49 No |
Answer / sunil makwana
#include<stdio.h>
#include<conio.h>
void searchval(int a[5],int n)
{
int i,temp=0,pos=0;
for (i=0; i<5; i++)
{
// printf("value of array:: %d",i);
if (a[i]==n)
{
temp=1;
pos=i;
}
}
if (temp==1)
{
printf("Element found %d , position==%d",n,pos);
}
else
{
printf("Element not found\n");
}
}
void main()
{
int a[5],i,n;
int ele,pos,temp;
clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);
searchval(a,ele);
getch();
}
Is This Answer Correct ? | 29 Yes | 17 No |
Answer / abdinasir maxamed abdulle
Array that display search using c languege
Is This Answer Correct ? | 16 Yes | 8 No |
Answer / mr. x
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[5],b,c,i;
clrscr();
printf("
Enter no. of elements: ");
scanf("%d",&c);
i=0;
while(i<c)
{
scanf("%d",&a[i]);
i++;
}
printf("
Enter the elements to be searched: ");
scanf("%d",&b);
i=0;
while(i<c &b!=a[i])
{
i++;
}
if (i<c)
{
printf("number found");
i++;
}
else
{
printf(" number not found");
}
getch();
}
Is This Answer Correct ? | 8 Yes | 5 No |
Answer / ghost
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,element,temp,pos;
clrscr();
printf("enter the array elements\n");
for (i=0; i<5; i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&element);
// searching for the element
for (i=0; i<5; i++)
{
if (a[i]==element)
{
temp=1;
pos=++i;
}
}
if (temp==1)
printf("%d Element found at position==%d",element,pos);
else
printf("Element not found\n");
getch();
} // end of main()
Is This Answer Correct ? | 13 Yes | 12 No |
can we change the default calling convention in c if yes than how.........?
What is the best way to store flag values in a program?
what is the significance of static storage class specifier?
What are the types of unary operators?
What is void main ()?
How can I open files mentioned on the command line, and parse option flags?
How will you delete a node in DLL?
how to construct a simulator keeping the logical boolean gates in c
What is a newline escape sequence?
Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)
Why is c known as a mother language?
What is the difference between array and pointer?