Write C program to put even and odd elements of array in two separate array



techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
include < stdio.h >

#define MAX_SIZE 1000 // Maximum size of the array

// Function declaration to print array
void printArray(int arr[], int len);

int main() {
int arr[MAX_SIZE];
int even[MAX_SIZE];
int odd[MAX_SIZE];

int evenCount, oddCount;
int i, size;

    // Reading size of the array
    printf("Enter size of the array: ");
    scanf("%d", & size);

   // Reading elements in array
    printf("Enter elements in the array: ");
   for (i = 0; i < size; i++) {
      scanf("%d", & arr[i]);
    }

    evenCount = 0;
    oddCount = 0;

        for (i = 0; i < size; i++) {
        // If arr[i] is odd
        if (arr[i] & 1) {
        odd[oddCount] = arr[i];
        oddCount++;
      } else {
        even[evenCount] = arr[i];
        evenCount++;
      }
    }

    printf("\nElements of even array: ");
    printArray(even, evenCount);

    printf("\nElements of odd array: ");
    printArray(odd, oddCount);

    return 0;
  }
    //Logic
void printArray(int arr[], int len) {
    int i;

    for (i = 0; i < len; i++) {
    printf("%d ", arr[i]);
  }
  printf("\n");
}

Result

C language

     





Related Topics




Popular Posts