KnowledgeBoat Logo

Computer Applications

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[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of both the arrays.

Java

Java Arrays

163 Likes

Answer

import java.util.Scanner;

public class KboatSDAEvenOdd
{
    public static void main(String args[]) {
        
        final int NUM_COUNT = 20;
        Scanner in = new Scanner(System.in);
        int i = 0;
        
        int arr[] = new int[NUM_COUNT];
        int even[] = new int[NUM_COUNT];
        int odd[] = new int[NUM_COUNT];
        
        System.out.println("Enter 20 numbers:");
        for (i = 0; i < NUM_COUNT; i++) {
            arr[i] = in.nextInt();
        }
        
        int eIdx = 0, oIdx = 0;
        for (i = 0; i < NUM_COUNT; i++) {
            if (arr[i] % 2 == 0)
                even[eIdx++] = arr[i];
            else
                odd[oIdx++] = arr[i];
        }
        
        System.out.println("Even Numbers:");
        for (i = 0; i < eIdx; i++) {
            System.out.print(even[i] + " ");
        }
        
        System.out.println("\nOdd Numbers:");
        for (i = 0; i < oIdx; i++) {
            System.out.print(odd[i] + " ");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of 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[ ] and all the odd numbers in another array odd[ ]. Finally, print the elements of both the arrays.

Answered By

49 Likes


Related Questions