42. JAVA Program to find a Number is exist in the given array or not.
How does this program work?
- In this program your going to learn about how to check a number is existing in array or not.
Here is the code
//Number existing array or not
import java.util.Scanner;public class array
{
public static void main(String[] args)
{
int n, x, flag = 0, i = 0;
System.out.print("Enter no.of elements you want in array:\n");
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:\n");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the element you want to find:\n");
x = s.nextInt();
for(i = 0; i < n; i++)
{
if(a[i] == x)
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
System.out.println("Element found at position:"+(i + 1));
}
{
System.out.println("Element not found");
}
}