C Program To Split Even and Odd Elements of An Array Into Two Arrays

Lets write a C program to divide or split even and odd elements of an array into two separate arrays.

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

Array elements of even[5] are …
2
4
6
8
10

Array elements of odd[5] are …
1
3
5
7
9

Visual Representation

Split even and odd elements of array

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array

Video Tutorial: C Program To Split Even and Odd Elements of An Array Into Two Arrays


[youtube https://www.youtube.com/watch?v=lQCvBWMSvHY]

YouTube Link: https://www.youtube.com/watch?v=lQCvBWMSvHY [Watch the Video In Full Screen.]

Source Code: C Program To Split Even and Odd Elements of An Array Into Two Arrays

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\n", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\n", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19

Array elements of even[5] are …
10
12
14
16
18

Array elements of odd[5] are …
11
13
15
17
19

Note: We’re assigning same size to all 3 array variables, because if user enters all odd numbers, then we need to transfer all the elements to array variable odd, for that we need the size of array variable odd to be same as that of the original array. Similarly, if user enters all even numbers we’ll need to have the same size for array variable even.

Logic To Split Even and Odd Elements of An Array Into Two Arrays

First we accept all the elements of an array from the user. Next we iterate through the array elements one by one using a for loop. Inside this for loop we check each individual element, if its even or odd. If a number is perfectly divisible by 2, then its even number else its odd number. If the fetched/selected number is even number, then we copy that number into array variable even, else we copy the element into array variable odd.

Next we use for loop to display the elements of array even and odd, which will have the even and odd elements of the original array.

Method 2

#include<stdio.h>
#include

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\t", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\t", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
11
13
15
16

Array elements of even[4] are …
2
4
6
16

Array elements of odd[6] are …
1
3
5
11
13
15

In above source code we check if the number input by the user is even or odd immediately after the user enters a number. We don’t wait until user inputs all the elements of the array. This way we eleminate the need for a separate for loop to check even and odd elements in the original array and then assigning it to array variables even and odd. This is the best approach, which reduces resource usage and has faster execution time.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Number of Positive, Negative and Zeros In An Array

Lets write a C program to count number of positive, negative and zeros in an Array.

Logic

number scale
If input number is greater than 0, then its positive number. If input number is less than 0, then its negative. If its neither greater than 0, nor less than 0, then the input number must be 0.

Related Read:
Number is Positive or Negative or Zero: C Program

Example: Expected Input/Output

Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Visual Representation

count positive negative zero in an array

Video Tutorial: C Program To Count Number of Positive, Negative and Zeros In An Array


[youtube https://www.youtube.com/watch?v=Z1mES-0NM-E]

YouTube Link: https://www.youtube.com/watch?v=Z1mES-0NM-E [Watch the Video In Full Screen.]

Source Code: C Program To Count Number of Positive, Negative and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4

Positive no: 4
Negative no: 5
Zeros: 1

Logic To Find Number of Positive, Negative and Zeros In An Array

First we initialize 0 to variables p, n and z – which represents positive, negative and zero. We accept 10 integer numbers from the user. Next we iterate through the array using for loop and check if the fetched array element is greater than 0, which means its a positive number, so we increment the value of variable p by one. If fetched array element is less than 0, which means its a negative number, so we increment the value of variable n by one. If both the cases fail, then the number must be zero, so we increment the value of z by 1.

After the completion of for loop execution, variables p, n and z will have the number of positive, negative and zeros present in the array.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, p = 0, n = 0, z = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] > 0)
            p++;
        else if(a[i] < 0)
            n++;
        else
            z++;
    }

    printf("\nPositive no: %d\nNegative no: %d\nZeros: %d\n", p, n, z);

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
0
-1
-2
-3
-4
-5

Positive no: 4
Negative no: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is greater than 0 or less than zero or is equal to zero, and increment the values of variable p, n and z accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. Less overhead and more efficient.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Number of Even, Odd and Zeros In An Array

Lets write a C program to count number of even, odd and zeros in an array.

What are Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Related Read:
Even or Odd Number: C Program

Example: Expected Output

Enter 10 integer numbers
10
15
0
-1
5
20
21
55
69
40

Even Numbers: 3
Odd Numbers: 6
Zeros: 1

Visual Representation

count even odd zero in an array

Video Tutorial: C Program To Count Number of Even, Odd and Zeros In An Array


[youtube https://www.youtube.com/watch?v=4iTxU655IR8]

YouTube Link: https://www.youtube.com/watch?v=4iTxU655IR8 [Watch the Video In Full Screen.]

Source Code: C Program To Count Number of Even, Odd and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, odd = 0, even = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven Numbers: %d\nOdd Numbers: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
9
8
7
6
5
4
3
2
1
0

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

Logic To Find Number of Even, Odd and Zeros In An Array

First we ask the user to enter N integer numbers. After that we iterate through the array elements one by one (using a for loop) and check if the fetched number is 0, if true, we’ll increment the value of zero by one. If the fetched element is not zero, then we check if its perfectly divisible by 2, if so, then its even number orelse its odd number – and we increment the values of variable even and odd accordingly.

Note: Make sure to first check if the fetched number is 0. Only after checking this, go further and check for even or odd conditions.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven No: %d\nOdd No: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
0
1
2
3
4
5
6
7
8
9

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is equal to zero or is perfectly divisible by 2 or it is not perfectly divisible by 2. Based on that we increment the values of variable zero, even and odd accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. So less overhead and more efficient.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Merge Two Arrays Alternatively

Lets write a C program to merge two arrays into third array in alternative position.

Example: Expected Output

Enter 5 elements for array a
10
12
14
16
18
Enter 5 elements for array b
11
13
15
17
19

Merging arrays a & b into c in alternate position
Array elements of c is:
10
11
12
13
14
15
16
17
18
19

Visual Representation of arrays a, b and c

Step 1: after first for loop
Copying array a to c
Step 2: after second for loop
Copying array b to c

Video Tutorial: C Program To Merge Two Arrays Alternatively


[youtube https://www.youtube.com/watch?v=Yu9QcnCC9RU]

YouTube Link: https://www.youtube.com/watch?v=Yu9QcnCC9RU [Watch the Video In Full Screen.]

Source Code: C Program To Merge Two Arrays Alternatively

Method 1: Array a and b are of same size

#include<stdio.h>

#define N 5
#define M (N * 2)

int main()
{
    int a[N], b[N], c[M], i, k;

    printf("Enter %d elements for array a\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Enter %d elements for array b\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &b[i]);

    printf("\nMerging arrays a & b into c in alternate position\n");
    for(i = 0, k = 0; i < N; i++, k += 2)
        c[k] = a[i];

    for(i = 0, k = 1; i < N; i++, k += 2)
        c[k] = b[i];

    printf("Array elements of c is:\n");
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 5 elements for array a
0
2
4
6
8
Enter 5 elements for array b
1
3
5
7
9

Merging arrays a & b into c in alternate position
Array elements of c is:
0
1
2
3
4
5
6
7
8
9

Logic To Merge Arrays a and b into c in Alternate positions

Here array variables a and b have same size. To copy the elements of array variable a to c, we initialize i to 0 and k to o, and start assigning values from a[i] to k[k], we increment the value of i by 1 for each iteration of for loop, but we increment the value of k by 2 for each iteration of first for loop. This way we assign elements of a to the values present at index 0, 2, 4, 6, 8 of array variable c.

In the next for loop, we reset the value of i to 0, and k value is rest to 1. k value keeps incrementing by 2 for each iteration of for loop, while i value increments by 1 for each iteration of the for loop. So the values present at index 0, 1, 2, 3, 4 of variable b are copied to the index places of 1, 3, 5, 7, 9 of array variable c.

Method 2: Array a and b are of different size

#include<stdio.h>

#define N1 3
#define N2 8
#define M  ( (N1 > N2) ? (N1 * 2) : (N2 * 2) )

int main()
{
    int a[N1], b[N2], c[M] = {0}, i, k;

    printf("Enter %d elements for array a\n", N1);
    for(i = 0; i < N1; i++)
        scanf("%d", &a[i]);

    printf("Enter %d elements for array b\n", N2);
    for(i = 0; i < N2; i++)
        scanf("%d", &b[i]);

    printf("\nMerging arrays a & b into c in alternate position\n");
    for(i = 0, k = 0; i < N1; i++, k += 2)
        c[k] = a[i];

    for(i = 0, k = 1; i < N2; i++, k += 2)
        c[k] = b[i];

    printf("Array elements of c is:\n");
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 3 elements for array a
0
2
4
Enter 8 elements for array b
1
3
5
6
7
8
9
10

Merging arrays a & b into c in alternate position
Array elements of c is:
0
1
2
3
4
5
0
6
0
7
0
8
0
9
0
10

Logic To Merge 2 arrays(of different size) Into 3rd Array

As you can see we’re using macros to assign size to the arrays a and b. You can change the size and play around to check different output for different input sizes.

N1 is the size of array a, N2 is the size of array b. Please execute this program and modify the values of N1 to be bigger than N2 and next change N2 to be bigger than N1, and then make both N1 and N2 equal – and check the outputs.

Inside macro M, which is the size of resultant array(c[M]), we store double the size of the biggest array we’re merging into c. We are doing this because, the sizes of the arrays to be merged might be different and the remaining positions will be filled with garbage values. So instead we’ll be pre-filling zeros in the places where there are no elements to fill.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Concatenate Two Arrays

Lets write a C program to concatenate or append two arrays into a third array. Make use of macros to assign size of the arrays.

Example: Expected Output

Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 5 integer numbers, for second array
5
6
7
8
9

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
1
2
3
4
5
6
7
8
9
Concatenation of arrays

Video Tutorial: C Program To Concatenate Two Arrays


[youtube https://www.youtube.com/watch?v=xrqzVAnvCIY]

YouTube Link: https://www.youtube.com/watch?v=xrqzVAnvCIY [Watch the Video In Full Screen.]

Source Code: C Program To Concatenate Two Arrays

Method 1: Arrays with same size

#include<stdio.h>

#define N 5
#define M (N * 2)

int main()
{
    int a[N], b[N], c[M], i, index = 0;

    printf("Enter %d integer numbers, for first array\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Enter %d integer numbers, for second array\n", N);
    for(i = 0; i < N; i++)
            scanf("%d", &b[i]);

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
    for(i = 0; i < N; i++)
        c[index++] = a[i];

    for(i = 0; i < N; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 5 integer numbers, for first array
0
9
8
7
6
Enter 5 integer numbers, for second array
5
4
3
2
1

Merging a[5] and b[5] to form c[10] ..

Elements of c[10] is ..
0
9
8
7
6
5
4
3
2
1

Logic To Concatenate Two Array To Form Third Array

Here size of array a and b are same, so the for loops are same to accept elements of array a and b. We initialize variable index to 0. We write a for loop and iterate from 0 to 4, and copy the individual elements of array a to array c. Here index of array a and c varies from 0 to 4.

In the second for loop again we iterate the for loop from 0 to 4. This time value of variable index varies from 5 to 9, where as value of index of variable b varies from 0 to 4. So the individual elements of array b are copied to array c from index 5 to 9.

Method 2: Arrays with different size

#include<stdio.h>

#define N1 5
#define N2 6
#define M (N1 + N2)

int main()
{
    int a[N1], b[N2], c[M], i, index = 0;

    printf("Enter %d integer numbers, for first array\n", N1);
    for(i = 0; i < N1; i++)
        scanf("%d", &a[i]);

    printf("Enter %d integer numbers, for second array\n", N2);
    for(i = 0; i < N2; i++)
            scanf("%d", &b[i]);

    printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N1, N2, M);
    for(i = 0; i < N1; i++)
        c[index++] = a[i];

    for(i = 0; i < N2; i++)
        c[index++] = b[i];

    printf("\nElements of c[%d] is ..\n", M);
    for(i = 0; i < M; i++)
        printf("%d\n", c[i]);

    return 0;
}

Output:
Enter 5 integer numbers, for first array
0
1
2
3
4
Enter 6 integer numbers, for second array
5
6
7
8
9
10

Merging a[5] and b[6] to form c[11] ..

Elements of c[11] is ..
0
1
2
3
4
5
6
7
8
9
10

Here the logic to concatenate array elements is same, but only difference is the size of array variable a and b. So we need to be careful while writing conditions in for loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert