Input in java using Scanner
Example:
Input: 5 1 2 3 4 5 Output: [1, 2, 3, 4, 5]
Approach
Java: Using Scanner
import java.util.Arrays;import java.util.Scanner;public class ScannerInput {public static void main(String[] args) {Scanner s = new Scanner(System.in);int n = s.nextInt();int arr[] = new int[n];for (int i = 0; i < n; i++) {int x = s.nextInt();arr[i] = x;}System.out.println(Arrays.toString(arr));s.close();}}
Example:
Input: 5 1 2 3 4 5 Output: [1, 2, 3, 4, 5]
Approach
Java: Using Scanner
import java.util.Arrays;import java.util.Scanner;public class ScannerInput {public static void main(String[] args) {Scanner s = new Scanner(System.in);int n = s.nextInt();int arr[] = new int[n];for (int i = 0; i < n; i++) {int x = s.nextInt();arr[i] = x;}System.out.println(Arrays.toString(arr));s.close();}}
No comments:
Post a Comment