Search for a particular element in Array, in case it is found, exit from loop and display the search result.(Use break)



import java.util.*;
public class Find {
public static void main(String[] args) {
boolean flag = false;
int intArray[]=new int[3];
Scanner input = new Scanner(System.in);
for(int i = 0; i<intArray.length; i++)
{
System.out.println("Enter the number for the array");
intArray[i] = input.nextInt();
}
System.out.println("Array elements are");
for(int i=0; i<intArray.length; i++)
{
System.out.println(intArray[i]);
}
System.out.println("Type the Number For Find the Position");
int n = input.nextInt();
for(int i=0; i<intArray.length; i++)
{
if(n==intArray[i])
{
int j = i+1;
System.out.println("Number found at : "+ j + " "+ " th Coulumn");
flag = true;
break;
}
}
if(flag==false)
System.out.println("Number not found");
}

}


Post a Comment

Copyright © Rough Record. Designed by OddThemes