Advertisements
Advertisements
प्रश्न
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 |
कोड लेखन
Advertisements
उत्तर
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
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
