Opening Hours :7AM to 9PM
public class OddEvenNumbers { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.print("Odd Numbers: "); for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 != 0) { System.out.print(nums[i] + " "); } } System.out.print("\nEven Numbers: "); for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 == 0) { System.out.print(nums[i] + " "); } } } }This program uses two for loops to print the odd and even numbers from the input array. The first for loop iterates through the array and checks if each number is odd or even by using the modulus operator (%). If a number is odd, it is printed to the console. The second for loop does the same thing, but it checks if the number is even and prints it if it is.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class OddEvenNumbers { public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; List<Integer> intList = Arrays.stream(nums).boxed().collect(Collectors.toList()); System.out.println("Odd Numbers: " + intList.stream().filter(i -> i % 2 != 0).collect(Collectors.toList())); System.out.println("Even Numbers: " + intList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList())); } }In this case we first convert array to List using Arrays.stream() and boxed() method, then using filter() with lambda expression we filter the odd and even numbers, then using collect() method to get it in list format and finally print it.