Check entered number is present in array or not in java

Write a java program to accept n elements in an array.

Enter a number from the user and check whether the number is present in array or not.If present in array then print how many times that number is there.

package array;

import java.util.*;
public class numberexistance
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size,i,no,count=0;
        System.out.println("enter size::");
        size=s.nextInt();
        int a[]=new int[size];
        System.out.println("enter array elements::");
        for(i=0;i<size;i++)
        {
            a[i]=s.nextInt();
        }
        System.out.println("enter no::");
        no=s.nextInt();
        for(i=0;i<size;i++)
        {
            if(a[i]==no)
            {
                count++;
            }
        }
        
        if(count>0)
        {
            System.out.println("element found...."+count + "times");
        }
        else
        {
            System.out.println("element not found....");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *