-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestSmallestArray.java
More file actions
51 lines (45 loc) · 1.34 KB
/
LargestSmallestArray.java
File metadata and controls
51 lines (45 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Program to find the largest and smallest elements in an array:
* Write a Java program to find the largest and smallest elements in an array of integers.
*/
import java.util.Scanner;
public class LargestSmallestArray
{
public static void findLargestAndSmallest(int[] inputArray)
{
int min = inputArray[0];
int max = 0;
int temp;
for(int i=0; i<inputArray.length; i++)
{
if(inputArray[i]>max)
{
temp = inputArray[i];
inputArray[i] = max;
max = temp;
}
else if(inputArray[i]<min)
{
temp = inputArray[i];
inputArray[i] = min;
min = temp;
}
}
System.out.println("Largest in the input array is: "+max);
System.out.println("Smallest in the input array is: "+min);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n = sc.nextInt();
int[] inputArray = new int[n];
System.out.println("Enter the "+ n + " array elements: ");
for(int i=0; i<n; i++)
{
inputArray[i] = sc.nextInt();
}
sc.close();
findLargestAndSmallest(inputArray);
}
}