Find the maximum and minimum value in an array and multiply them
In this challenge, enter the value in one line as size of an array, and enter numbers separated by space as elements of array, find the maximum and minimum value of an array and multiply them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class PracticeDemo { | |
public static void main(String [] args){ | |
Scanner sc=new Scanner(System.in); | |
String arraySize=sc.nextLine(); | |
int size=Integer.parseInt(arraySize); | |
String elements=sc.nextLine(); | |
String [] array=new String [size]; | |
int j=0,x=0,maximum=0,minimum=0; | |
for(int i=0;i<elements.length();i++){ | |
if(String.valueOf(elements.charAt(i)).equals(" ")){ | |
array[j]=elements.substring(x,i); | |
x=i+1; | |
j++; | |
} | |
} | |
array[j]=elements.substring(x,elements.length()); | |
maximum=Integer.parseInt(array[0]); | |
minimum=Integer.parseInt(array[0]); | |
for(int k=0;k<array.length;k++){ | |
if(Integer.parseInt(array[k])>maximum) | |
maximum=Integer.parseInt(array[k]); | |
else if(Integer.parseInt(array[k])<minimum) | |
minimum=Integer.parseInt(array[k]); | |
} | |
System.out.print(maximum*minimum); | |
sc.close(); | |
} | |
} |
Output:
6
11 22 33 44 55 66
726
6
11 22 33 44 55 66
726
Comments
Post a Comment