Advertisements
Advertisements
प्रश्न
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.
संक्षेप में उत्तर
उत्तर
import java.util.*;
public class AQ8
{public static void main(String args[])
{
Scanner obj = new Scanner(System.in);
int arr[] = new int[20];
int even[] = new int[20];
int odd[] = new int[20];
int x = 0, y = 0;
// INPUT
System.out.println("Enter 20 int values");
for (int i = 0; i < arr.length; i++)
{
arr[i] = obj.nextInt();
if (arr[i] % 2 == 0)
even[x++] = arr[i]; // Array even] for storing even numbers
else
odd[y++] = arr[i]; // Array odd[] for storing odd numbers
}
// OUTPUT
System.out.print("\nElements in the Even number Array :");
for (int i = 0; i < x; i++)
System.out.print(even[i] + ",");
System.out.print("\nElements in the Odd number Array :");
for (int j = 0; j < y; j++)
System.out.print(odd[j] + ",");
}}
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?