English

Create an array RW[ ] of type int and size 8 and fill it with variables. Create another array EW[ ] that contains the even elements of RW[ ] in front followed by the odd elements.

Advertisements
Advertisements

Question

Write a program for the following.

Create an array RW[ ] of type int and size 8 and fill it with variables. Create another array EW[ ] that contains the even elements of RW[ ] in front followed by the odd elements.

For example:

Input RW[ ] 31 45 62 87 54 29 83 44
Output EW[ ] 62 54 44 31 45 87 29 83
Code Writing
Advertisements

Solution

import java.util.Scanner;
public class RearrangeEvenOdd 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);        
        int RW[] = new int[8];
        int EW[] = new int[8];        
        System.out.println("Enter 8 integers for array RW:");
        for (int j = 0; j < RW.length; j++) 
        {
            RW[j] = sc.nextInt();
        }
                int left = 0;
        int right = RW.length        
        for (int j = 0; j < RW.length; j++)
        {
            if (RW[j] % 2 == 0) 
           {
                EW[left] = RW[j];
                left++; 
            } else {
                EW[right] = RW[j];
                right--;  
            }
        }
        System.out.print("EW[ ]\t");
        for (int j = 0; j < EW.length; j++) 
        {
            System.out.print(EW[j] + "\t");
        }        
        sc.close();
    }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 7: Arrays - One Dimension - Exercises [Page 177]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 7 Arrays - One Dimension
Exercises | Q 4. (v) | Page 177
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×