Computer Science, asked by aishwaryadcse, 2 months ago

Write a JAVA Program to find the index of a specific number kin a sorted array. If the number is repeated more than once print the index of first
occurrence of an element​

Answers

Answered by anindyaadhikari13
3

Answer:

The given co‎de is written in Java.

import java.util.*;

public class Index  {

   public static void main(String args[])  {

       Scanner sc=new Scanner(System.in);

       int i,search,n;

       boolean x=true;

       System.out.print("How many elements? ");

       n=sc.nextInt();

       int a[]=new int[n];

       System.out.println("Enter them..");

       for(i=0;i<n;i++)    {

           System.out.print("a["+i+"] = ");

           a[i]=sc.nextInt();

       }

       Arrays.sort(a);

       System.out.println("Sorted Array: "+Arrays.toString(a));

       System.out.print("Enter the element to be searched for: ");

       search=sc.nextInt();

       for(i=0;i<n;i++)    {

           if(a[i]==search)    {

               System.out.println("Position: "+i);

               x=false;

               break;

           }

       }

       if(x)

           System.out.println("Not found in the array.");

       sc.close();

   }

}

See the attachment for output.

•••♪

Attachments:
Answered by kamalrajatjoshi94
0

Answer:

Refer to the attachment for undestanding

  • Sorry some photos are not sorted I think the program is not sorted rest others sorted.
  • I gave the output for a element that is found
  • For a element that is not found
  • For a element whose number is repeated more than once
  • I used Bubble sort to sort the array
Attachments:
Similar questions