Week2,1
// Importing required packages import java.util.Scanner; import java.util.Arrays; // Drivers Code public class lab1{ public static void main(String args[]){ // Using a scanner object to use Scanner scanner = new Scanner(System.in); // Intitializing an array as per CLA input int arr[]= new int[Integer.parseInt(args[0])]; // Traversing the array to take the input from the user for(int i=0;i<arr.length;i++){ System.out.print("Enter the value at index "+i+" : "); arr[i] = scanner.nextInt(); } // Sorting the array using builtin sort method Arrays.sort(arr); // Closing the object to free up the memory scanner.close(); // Traversing the array to print the output onto the screen System.out.print("Items in the array are : "); for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } } }
Leave a Comment