Friday 10 November 2017

C Program to Put Even & Odd Elements of an Array in 2 Separate Arrays


Code:

#include

void main()
{
    long int ARR[10], OAR[10], EAR[10];
    int i, j = 0, k = 0, n;

    printf("Enter the size of array AR \n");
    scanf("%d", &n);
    printf("Enter the elements of the array \n");
    for (i = 0; i < n; i++)
    {
        scanf("%ld", &ARR[i]);
        fflush(stdin);
    }
    /*  Copy odd and even elements into their respective arrays */
    for (i = 0; i < n; i++)
    {
        if (ARR[i] % 2 == 0)
        {
            EAR[j] = ARR[i];
            j++;
        }
        else
        {
            OAR[k] = ARR[i];
            k++;
        }
    }
    printf("The elements of OAR are \n");
    for (i = 0; i < k; i++)
    {
        printf("%ld\n", OAR[i]);
    }
    printf("The elements of EAR are \n");
    for (i = 0; i < j; i++)
    {
        printf("%ld\n", EAR[i]);
    }
}

Output:

Enter the size of array AR
6
Enter the elements of the array
12
15
99
60
39
49
The elements of OAR are
15
99
39
49
The elements of EAR are
12

60

More C Programs:









100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...