0% found this document useful (0 votes)
10 views5 pages

Program Forschool

The document discusses a Java program to search for an element in an array. The program takes input of array size and elements, allows input of element to search for, and searches the array to return the index if found or -1 if not found.

Uploaded by

Siddhi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Program Forschool

The document discusses a Java program to search for an element in an array. The program takes input of array size and elements, allows input of element to search for, and searches the array to return the index if found or -1 if not found.

Uploaded by

Siddhi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q 1) WAP to input an array and search for an element

wither it is in the array or not , if yes then give the


position where it is in the array using methods and
function .
o Step 1: Traverse the array
o Step 2: Match the key element with array element
o Step 3: If key element is found, return the index position of the array element
o Step 4: If key element is not found, return -1

PROGRAM
import java.util.*;

class xyz

int a[] , n ;

public xyz (int n)

{ this.n=n ;

a = new int[n] ;

public void input ()

Scanner sc = new Scanner(System.in);

int k ;
for(k=0 ; k<n ; k++)

a[k] =sc.nextInt();

public void display()

int t ;

System.out.print(" array is " );

for ( t=0 ; t< n; t++)

System.out.print(a[t]+ ",");

System.out.println();

public void Search( int s)

int w, f =-1 ;

for ( w= 0 ; w<n ; w++)


{

if (a[w]== s)

f=w;

break;

if(f<0)

System.out.println(s+" is not found");

else

System.out.println(s+" is found at"+(f+1) +" position");

public static void main()

Scanner sc = new Scanner(System.in);

int m ,e ;

System.out.print(" enter size of array");

m = sc.nextInt();

xyz ob = new xyz(m);

ob.input();
System.out.print(" enter searching element");

e = sc.nextInt();

ob.Search(e);

OUTPUT :
enter size of array10
4
6
33
55
99
45
65
25
69
10
enter searching element 69
69 is found at 9 position
VDD Table
Data Type Description
Integer a to input the array
Integer n to enter the size of
The array
Integer s to enter the number
To b searched
Integer w, k, t used for loop

You might also like