

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to split the Even and Odd elements into two different lists
To split the Even and Odd elements into two different lists, the Java code is as follows −
Example
import java.util.Scanner; public class Demo{ public static void main(String[] args){ int n, j = 0, k = 0; Scanner s = new Scanner(System.in); System.out.println("Enter the number of elements required :"); n = s.nextInt(); int my_arr[] = new int[n]; int odd_vals[] = new int[n]; int even_vals[] = new int[n]; System.out.println("Enter the elements of the array(even and add numbers) :"); for(int i = 0; i < n; i++){ my_arr[i] = s.nextInt(); } for(int i = 0; i < n; i++){ if(my_arr[i] % 2 != 0){ odd_vals[j] = my_arr[i]; j++; } else { even_vals[k] = my_arr[i]; k++; } } System.out.print("The odd numbers in the array : "); if(j > 1){ for(int i = 0;i < (j-1); i++){ if(odd_vals[i]==1){ System.out.println("1 is niether even nor odd"); } else System.out.print(odd_vals[i]+","); } System.out.print(odd_vals[j-1]); } else { System.out.println("There are no odd numbers."); } System.out.println(""); System.out.print("The even numbers in the array : "); if(k > 1){ for(int i = 0; i < (k-1); i++){ if(even_vals[i]==1){ System.out.println("1 is niether even nor odd"); } else System.out.print(even_vals[i]+","); } System.out.print(even_vals[k-1]); } else { System.out.println("There are no even numbers in the array."); } } }
Output
Enter the number of elements required : Enter the elements of the array(even and add numbers) : The odd numbers in the array : 1 is niether even nor odd 9 The even numbers in the array : 2,4,6
Console input
5 1 2 4 6 9
A class named ‘Demo’ contains the main function that asks for the number of elements that should be stored in the array and declares two new arrays that will store the odd values and even values respectively. The array elements are taken from the user and a ‘for’ loop is run to check if the number is divisible by 0, i.e checking if the remainder when the number is divided by 2 is 0. If yes, then that number from the main array will be stored in the even array, and in the odd array otherwise. Since 1 is neither even nor odd, it prints the message while storing 1 in the even or odd array.
- Related Questions & Answers
- Python program to split the even and odd elements into two different lists.
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- C# program to split the Even and Odd integers into different arrays
- Java Program to Split a list into Two Halves
- Sorting odd and even elements separately JavaScript
- C program to store even, odd and prime numbers into separate files
- Simple way to find if two different lists contain exactly the same elements in Java
- Swap Even Index Elements And Odd Index Elements in Python
- Java Program to Join Two Lists
- Java Program to Merge two lists
- Java program to print odd and even number from a Java array.
- Java program to Print Odd and Even Number from an Array
- Missing even and odd elements from the given arrays in C++
- C# program to print all the common elements of two lists
- Python program to print all the common elements of two lists.
Advertisements