create an array of size n.Accept elements in "input" function and display elements in "display" function."displaymin"and "displaymax" to display the minimum and the maximum number using Java.Please answer fast.No wrong answer.
Answers
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter size of array= ");
n=sc.nextInt();
int[]a=new int[n];
System.out.println("Enter array elements= ");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<a.length;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
for(int i=0;i<a.length;i++)
{
if(a[i]<min)
{
min=a[i];
}
}
System.out.println("Maximum= "+max);
System.out.println("Minimum= "+min);
}
}
Answer:
import java.util.*;
import java.util.Collections;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Integer[]numbers;
System.out.print("Enter size of an array:");
int size = in.nextInt();
numbers=new Integer[size];
System.out.println("Enter "+size+" elements");
for (int i=0;i<=size-1 ;i++ )
{
System.out.print("Enter "+i+" element:");
numbers[i]=in.nextInt();
}
DisplayMin(numbers);
DisplayMax(numbers);
}
public static void DisplayMin(Integer[]numbers)
{
int min = Collections.min(Arrays.asList(numbers));
System.out.println("Minimum number of array is : " + min);
}
public static void DisplayMax(Integer[]numbers)
{
int max = Collections.max(Arrays.asList(numbers));
System.out.println("Maximum number of array is : " + max);
}
}
Explanation: