Computer Science, asked by adritaghosh232k4, 6 months ago

Write a program in Java to accept 20 numbers in a single dimensional array arr[20].
Transfer and store all the even numbers in an array even[ 1 and all the odd numbers
in another array odd[ ]. Finally, print the elements of both the arrays.​

Answers

Answered by AkshayTawari11
2

Answer:

Java Program to Put Even & Odd Elements of an Array in 2 Separate Arrays. public class Even_Odd. int n, j = 0, k = 0; Scanner s = new Scanner(System. System. out. print("Enter no. of elements you want in array:"); n = s. nextInt(); int a[] = new int[n]; int odd[] = new int[n]; int even[] = new int[n];

Answered by anindyaadhikari13
4

Answer:

Here comes the program for the question.

import java.util.*;

public class Java {

 public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    int arr[]=new int[20], i;

    ArrayList<Integer> even=new ArrayList<Integer>();

    ArrayList<Integer> odd=new ArrayList<Integer>();

    System.out.println("Enter 20 elements..");

    for(i=0;i<20;i++)  {

       System.out.print("Enter: ");

       arr[i]=sc.nextInt();

       if(arr[i]%2==0)

          even.add(arr[i]);

       else

          odd.add(arr[i]);

    }

    System.out.println("Given Array:\n "+Arrays.toString(arr));

    System.out.println("Even Numbers in Array:\n"+even);

    System.out.println("Odd Numbers in Array:\n"+odd);

    sc.close();

 }

}

Explanation:

  • At first, ask the user to enter the numbers in array.
  • After entering, I have created two dynamic arrays named even and odd which stores the even and odd number from the array.
  • As the total number of even and odds present cannot be counted, so I have created dynamic array. Advantage is that size of dynamic array can be increased or decreased and elements can also be deleted. Now, i have checked if the number entered is odd or even. If odd, add the number to odd array or else, add the number to even array.

See the attachment for output .

Attachments:
Similar questions