Python/C/C++/JAVA

Moderate Practice Programs with Code and Concept

By D.S

Separate Even or Odd Numbers from an Array and Lists

In programming, an array is a collection of similar data types that are stored in a contiguous memory space. Separating even and odd numbers within an array can be achieved through various algorithms and syntaxes in different programming languages such as C, C++, Python, and Java. One popular approach to this problem involves iterating through the array using loops and conditional statements to check for even or odd values using modulo arithmetic. Once identified, these values can then be stored in separate arrays or printed out accordingly. Efficient programming is critical when working with large arrays or datasets, so it's important to optimize time complexity algorithms wherever possible to ensure optimal performance and speed of execution. Ultimately, the method used will depend on the specific needs and constraints of the project at hand.

(a.) C Code

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elements:\n");
    for(int i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
    printf("Even numbers: ");
    for(int i = 0; i < n; i++) {
        if(arr[i] % 2 == 0)
            printf("%d ", arr[i]);
    }
    
    printf("\nOdd numbers: ");
    for(int i = 0; i < n; i++) {
        if(arr[i] % 2 != 0)
            printf("%d ", arr[i]);
    }
    
    return 0;
}
Output:-
Enter the number of elements: 5
Enter the elements:
1 2 3 4 5
Even numbers: 2 4
Odd numbers: 1 3 5

(b.) C++ Code

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements:" << endl;
    for(int i = 0; i < n; i++)
        cin >> arr[i];
    
    cout << "Even numbers: ";
    for(int i = 0; i < n; i++) {
        if(arr[i] % 2 == 0)
            cout << arr[i] << " ";
    }
    
    cout << "\nOdd numbers: ";
    for(int i = 0; i < n; i++) {
        if(arr[i] % 2 != 0)
            cout << arr[i] << " ";
    }
    
    return 0;
}
Output:-
Enter the number of elements: 5
Enter the elements:
1 2 3 4 5
Even numbers: 2 4
Odd numbers: 1 3 5

(c.) Python Code

def main():
    n = int(input("Enter the number of elements: "))
    arr = list(map(int, input("Enter the elements: ").split()))
    
    print("Even numbers:", end=" ")
    for num in arr:
        if num % 2 == 0:
            print(num, end=" ")
    
    print("\nOdd numbers:", end=" ")
    for num in arr:
        if num % 2 != 0:
            print(num, end=" ")

if __name__ == "__main__":
    main()
Output:-
Enter the number of elements: 5
Enter the elements:
1 2 3 4 5
Even numbers: 2 4
Odd numbers: 1 3 5

(d.) Java Code

import java.util.Scanner;

      public class Main {
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              System.out.print("Enter the number of elements: ");
              int n = scanner.nextInt();
              int[] arr = new int[n];
              
              System.out.println("Enter the elements:");
              for (int i = 0; i < n; i++) {
                  arr[i] = scanner.nextInt();
              }
              
              System.out.print("Even numbers: ");
              for (int i = 0; i < n; i++) {
                  if (arr[i] % 2 == 0) {
                      System.out.print(arr[i] + " ");
                  }
              }
              
              System.out.print("
Odd numbers: ");
              for (int i = 0; i < n; i++) {
                  if (arr[i] % 2 != 0) {
                      System.out.print(arr[i] + " ");
                  }
              }
          }
      }
Output:-
Enter the number of elements: 5
Enter the elements:
1 2 3 4 5
Even numbers: 2 4
Odd numbers: 1 3 5