Friday, May 3, 2019

Searching an Array

Write a C program to search for an element �a� in the array. (Linear Search) 

Input Format: 
Input consists of n+2 integers. The first integer corresponds to �n� , the size of the array. The next �n� integers correspond to the elements in the array. The last integer corresponds to �a�, the element to be searched. 
Assume that the maximum size of the array is 20. 

Output Format: 
Refer sample output for details. 
Sample Input 1: 
3

Sample Output 1: 
 6 is present in the array 

Sample Input 2: 
 5 
 2 
 3 
 6 
 8 
 1 
 9 
 Sample Output 2:
 9 is not present in the array

Code:
  #include<stdio.h>
int main(void){
int a[20], find, size,i;
scanf("%d",&size);
for(i=1;i<=size;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&find);
for(i=0;i<size;i++)
{
if(a[i]==find){
printf("%d is present in the array",find);
break;
}
}
if(i==size)
{
printf("%d is not present in the array",find);
}
return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...