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.
#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;
}
#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;
}
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()
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] + " ");
}
}
}
}