Untitled

 avatar
unknown
java
4 years ago
1.2 kB
5
Indexable
{

    static void halfDivider(int len, int[] list) {
        int sdlen; //len of half list
        //finding the len of half array for being odd or even
        if (len % 2 == 1) {
            sdlen = len / 2 + 1;
        } else {
            sdlen = len / 2;
        }

        
        int[] nlist = new int[sdlen];
        System.out.print("[");
        //writing and printing the half of first array to second
        for (int i = 0; i < sdlen; i++) {
            nlist[i] = list[i];
            System.out.print(nlist[i]);
            if (i != sdlen - 1) {
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

    public static void main(String[] args) {


        int len;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of inputs :");
        len = scan.nextInt(); //len of array
        int[] list = new int[len];
        System.out.println("Enter the numbers :");
        //scanning integers from user by using indexes
        for (int i = 0; i < len; i++) {
            list[i] = scan.nextInt(); 
        }
        halfDivider(len, list);

    }
}
Editor is loading...