wap to find the index of largest number in an array using scanner class. If the largest number is repeated more than once print the index of first occurrence of an element in java
Answers
Answered by
3
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,max=0,index=0;
System.out.println("Enter the size of the array:");
n=in.nextInt();
int a[]=new int[n];
System.out.println("Enter the 1st element of the array:");
a[0]=in.nextInt();
System.out.println("Enter the remaining elements of the array");
for(int i=1;i<n;i++)
{
a[i]=in.nextInt();
if(a[i]>max)
{
max=a[i];
index=i;
}
}
System.out.println("The maximum element:"+max);
System.out.println("The index of the maximum first occurence element:"+index);
in.close();
}
}
- The first output is attached in the first 2 photos.
- The second output is attached in the next 2 photos.
Attachments:
Similar questions