create an array of twenty integers and fill the array with even numbers Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of create an array of twenty integers and fill the array with even numbers without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to create an array that prints out the first 20 even numbers but I don’t know how to get the arrays to print them and I am only getting an array of twenty 0. So far this is what I have written.

int[] mylist = new int[20];
int even = 0;   

while ((even%2 == 0) && (even < 20)){
    even++;
}

System.out.println("Element at index 0: " + mylist[0]);
System.out.println("Element at index 1: " + mylist[1]);
System.out.println("Element at index 2: " + mylist[2]);
System.out.println("Element at index 3: " + mylist[3]);
...

Edit: This is what I have done so far:

    int[] mylist=new int[20];//establishes the name and length of an array

    for (int even=0;even<mylist.length;even++)

    {
    mylist[even]=even;

        if(even%2==0)
     System.out.println("Even Element: "+even);
}

However, instead of printing out twenty even numbers it is just printing out the even numbers between 1 and 20.

Answer

create an array of twenty integers and fill the array with even numbers

  1. Create an array of 20 int elements:

    int[] evenNumbers = new int[20];
    
  2. Fill the array with even numbers:

    for (int i = 0; i < evenNumbers.length; i++) {
       evenNumbers[i] = i*2;
    }
    
  3. Print the elements of the array

    for (int i = 0; i < evenNumbers.length; i++) {
        System.out.println("Element at index " + i + ": " + evenNumbers[i]);
    }
    
We are here to answer your question about create an array of twenty integers and fill the array with even numbers - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji