Add OOP/Java/Lab/Week4/LargeSmall.java

This commit is contained in:
Aadit Agrawal 2024-08-31 01:50:30 +05:30
parent 829ba30125
commit 40105a6a7f

View File

@ -0,0 +1,31 @@
import java.util.Scanner;
class LargeSmall {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter " + size + " integers:");
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
int largest = arr[0];
int smallest = arr[0];
for (int num : arr) {
if (num > largest) {
largest = num;
}
if (num < smallest) {
smallest = num;
}
}
System.out.println("Largest element in the array: " + largest);
System.out.println("Smallest element in the array: " + smallest);
sc.close();
}
}