-
Notifications
You must be signed in to change notification settings - Fork 0
/
Largest_Smallest.java
34 lines (32 loc) · 1.05 KB
/
Largest_Smallest.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
import java.util.Scanner;
public class Largest_Smallest {
public static void main(String[] args) {
//declare scanner object
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
System.out.println("Enter numbers:");
for (int i=0;i<n;i++) {
int num = scan.nextInt();
arr[i] = num;
// System.out.println("number = "+arr[i]);
}
//closing scanner class
scan.close();
// System.out.print(Arrays.toString(arr));
int large =0;//assuming largest value to 0
int small = arr[0];//assuming smallest value to 0
for(int j=0;j<n;j++){
//for largest
if(large<arr[j]) {
large = arr[j];
}
//for smallest
if(small>arr[j]) {
small = arr[j];
}
}
System.out.println("The largest number is: "+large);
System.out.println("The smallest number is: "+small);
}
}