C program to create an integer array and store the EVEN and ODD elements in a different array

Here, we are going to learn how to create an integer array and store the EVEN and ODD elements in a different array in C programming language?
Submitted by Nidhi, on July 22, 2021

Problem Solution:

Here, we will create an integer array then find and store EVEN and ODD elements in two different separate arrays and print them.

Program:

The source code to create an integer array and store the EVEN and ODD elements in the different arrays is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to create an integer array and store
// the EVEN and ODD elements in a different array

#include <stdio.h>

int main()
{
    int arr[6];
    int evenArr[6];
    int oddArr[6];

    int i = 0, j = 0, k = 0;

    printf("Enter the array elements: ");
    for (i = 0; i < 6; i++)
        scanf("%d", &arr[i]);

    for (i = 0; i < 6; i++) {
        if (arr[i] % 2 == 0)
            evenArr[j++] = arr[i];
        else
            oddArr[k++] = arr[i];
    }

    printf("Even elements: ");
    for (i = 0; i < j; i++)
        printf("%d ", evenArr[i]);

    printf("\nOdd elements: ");
    for (i = 0; i < k; i++)
        printf("%d ", oddArr[i]);

    printf("\n");
    return 0;
}

Output:

RUN 1:
Enter the array elements: 10 20 30 40 55 77
Even elements: 10 20 30 40 
Odd elements: 55 77 

RUN 2:
Enter the array elements: 11 22 33 44 55 66
Even elements: 22 44 66 
Odd elements: 11 33 55

RUN 3:
Enter the array elements: 10 20 30 40 50 60
Even elements: 10 20 30 40 50 60 
Odd elements: 

RUN 4:
Enter the array elements: 11 33 55 77 99 9
Even elements: 
Odd elements: 11 33 55 77 99 9 

Explanation:

In the main() function, we created an array of 6 integer elements then we read array elements from the user and found the EVEN and ODD numbers and store them into evenArr and oddArr arrays and printed the result on the console screen.

C One-Dimensional Array Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.