From 40105a6a7fa7e76b041399408a09806007f80986 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 31 Aug 2024 01:50:30 +0530 Subject: [PATCH] Add OOP/Java/Lab/Week4/LargeSmall.java --- OOP/Java/Lab/Week4/LargeSmall.java | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 OOP/Java/Lab/Week4/LargeSmall.java diff --git a/OOP/Java/Lab/Week4/LargeSmall.java b/OOP/Java/Lab/Week4/LargeSmall.java new file mode 100644 index 0000000..e1676d0 --- /dev/null +++ b/OOP/Java/Lab/Week4/LargeSmall.java @@ -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(); + } +}