-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallestLargest.java
57 lines (46 loc) · 1.43 KB
/
smallestLargest.java
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
52
53
54
55
56
57
/*
# finding largest & smallest integer in a user inputed array.
steps:
-> take array size input and initialize array
-> take array elements input
-> call findLargest & findSmallest to evaluate largest and smallest
-> the functions will printout there findings
*/
import java.util.Scanner;
public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.print ("Number of integers? : ");
int size = sc.nextInt();
int[] integers = new int [size];
System.out.printf ("Enter %d integers: ", size);
for (int i = 0; i < size; i++)
{
integers [i] = sc.nextInt();
}
findLargest (integers);
findSmallest (integers);
}
public static void findSmallest (int array[])
{
int smallest = (int)Double.POSITIVE_INFINITY;
for (int i = 0; i < array.length; i++)
{
if (array [i] < smallest)
smallest = array [i];
}
System.out.println ("Smallest value: " + smallest);
}
public static void findLargest (int array[])
{
int largest = (int)Double.NEGATIVE_INFINITY;
for (int i = 0; i < array.length; i++)
{
if (array [i] > largest)
largest = array [i];
}
System.out.println ("Largest value: " + largest);
}
}