Class 10 ICSE - Java Array Searching Programs

Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1: Write a program to enter numbers in array and find the desired number using linear search and display its position.
Solution:
import java.util.*;
public class Search_Element
{
    public static void main(String[] args) 
    {
        int n, x, flag = 0, i = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int[] a = new int[n];
        System.out.println("Enter all the elements:");
        for(i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        System.out.print("Enter the element you want to find:");
        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));
        }
        else
        {
            System.out.println("Element not found");
        }
    }
}
                        

In the above example, we have created a array of size n and displaying the searched element along with its position.
Program 2: Write a program to input names of the cities and find using linear search whether the specific city is present in an array or not, if present then display its position .
Solution:
import java.util.*;
public class Search_City
{
    public static void main(String[] args) 
    {
        int n, flag = 0, i = 0;  String s;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = sc.nextInt();
        String[] a = new String[n];
        System.out.println("Enter all the elements:");
        for(i = 0; i < n; i++)
        {
            a[i] = sc.next(); //entering array elements
        }
        System.out.print("Enter the element you want to find:");
        s = sc.next(); //entering the element to be searched 
        for(i = 0; i < n; i++) //linear searching
        {
            if(a[i].equals(s)) //comparing each element with the with the element to be searched
            {
                flag = 1;
                break;
            }
            else
            {
                flag = 0;
            }
        }
        if(flag == 1)
        {
            System.out.println("Element found at position:"+(i + 1));
        }
        else
        {
            System.out.println("Element not found");
        }
    }
}
                        

In the above example, we have created a array of size n and searching the city whether it is present or not if present then displaying its position.
Program 3: Write a program to enter numbers in array and find the desired number using binary search and display its position.
Solution:
import java.util.*;
class BinarySearch{
 public static void binarySearch(int arr[], int low, int high, int k){
   int mid = (low + high)/2;
   while( low <= high )
   {
      if ( arr[mid] < k ){
        low = mid + 1;   
      }
      else if ( arr[mid] == k ){
        System.out.println("Element is found at position: " + (mid+1));
        break;
      }
      else{
         high = mid - 1;
      }
      mid = (low + high)/2;
   }
   if ( low > high ){
      System.out.println("Element is not found!");
   }
 }
 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter no. of elements you want in array:");
  int n= sc.nextInt();
  int arr[] = new int[n];
  System.out.println("Enter all the elements:");
  for(int i = 0; i < n; i++)
        {
            arr[i] = sc.nextInt();
        }
  System.out.print("Enter the element you want to find:");
  int k = sc.nextInt();
  int high=n-1;
  int low =0;
  binarySearch(arr,low,high,k);    
 }
}

                        

In the above example, we have created a array of size n and checking whether the element is found or not , if found then displaying its position
Program 4: Write a program to input names of the states and find using binary search whether the specific state is present in an array or not, if present then display its position .
Solution:
import java.util.*;
class BinarySearch
{
    public static void main(String args[])
    {
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter the size of array");
        int n= sc.nextInt();
        String[] str = new String[n];
        System.out.println("Enter array elements");
        
         for (int i = 0; i < n; i++) 
        {
        str[i]=sc.next();
        }
        System.out.println("Enter element to be searched");
        String x=sc.next();
        int low=0,high=str.length-1, mid=0;
        mid= (low+high)/2;
        while(low<=high)
        {
            if (str[mid].compareTo(x) < 0) 
            { 
                low = mid + 1; 
            } 
            else if (str[mid].compareTo(x) > 0) 
            {
		high = mid - 1;
	    } 
	    else 
	    {
	         System.out.println("Element is found at position: " + (mid+1));
                 break;
	    }
        }
        if ( low > high )
        {
                 System.out.println("Element is not found!");
        }
    }
}
                        

In the above example, we have created a array of size n and checking whether the inputed state is present in array or not if present then displaying its position.

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com