WILL GIVE BRAINLIEST! How do I create (and then print out) an int array of length n filled with 0's in Java? Please hurry, my homework assignment is due tomorrow. Thank you to anyone who actually helps!

Respuesta :

Answer:

import java.util.Scanner;

class Main {  

 public static void main(String args[]) {

   Scanner in = new Scanner(System.in);

   System.out.print("Enter Length Of Array: ");

   int N = in.nextInt();

   int zeroArr[] = new int [N];

   // Create Zero Array

    for (int i = 0; i < N ; i ++){

       zeroArr[i] = 0;

     }

   // Print Zero Array

   for (int i = 0; i < N ; i ++){

       System.out.print(zeroArr[i]);

     }

}

}

Required Program :-

import java.util.Arrays;

public class x{

public static void main(String args[]){

int i, n = 5;

int arr[] = new arr[n];

System.out.println(Arrays.toString(arr));

}

}

Note :- If we declare array and don't initialise with values, all the elements of the array are by default considered 0.

  • Arrays.toString() is a function present in java.util.Arrays class that prints an array

Output: [0, 0, 0, 0, 0]