JAVA SP T


Lets dive into snapshot of my programs!💻

14.Program to Print Odd and Even Numbers from an array

💡Code:

 public class OddEvenNumbers7078 {
              public static void main(String[] args) {
                  // Example array
                  int[] numbers = {15, 7, 23, 45, 9, 56, 82, 31};
          
                  // Print odd and even numbers from the array
                  System.out.println("Odd numbers:");
                  printOddNumbers(numbers);
          
                  System.out.println("\nEven numbers:");
                  printEvenNumbers(numbers);
              }
          
              // Function to print odd numbers from an array
              private static void printOddNumbers(int[] array) {
                  for (int num : array) {
                      if (num % 2 != 0) {
                          System.out.print(num + " ");
                      }
                  }
              }
          
              // Function to print even numbers from an array
              private static void printEvenNumbers(int[] array) {
                  for (int num : array) {
                      if (num % 2 == 0) {
                          System.out.print(num + " ");
                      }
                  }
              }
          }
           

📸Output :

output