C Program To Re-arrange Even and Odd Elements of An Array

Lets write a C program to re-arrange even and odd elements of an array.

Note: We need to arrange EVEN numbers at the top and ODD numbers to the bottom of the same array.

Example: Expected Input/Output

Enter 5 integer numbers
11
10
13
12
15

After re-arranging even and odd elements …
10
12
13
11
15

Visual Representation

Rearranging even and odd elements of an array

Video Tutorial: C Program To Re-arrange Even and Odd Elements of An Array


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

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

Source Code: C Program To Re-arrange Even and Odd Elements of An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, j = N, temp;

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

    for(i = 0; i <= j; i++)
    {
        if(a[i] % 2 != 0)
        {
            while(j > i)
            {
                j--;
                if(a[j] % 2 == 0)
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                    break;
                }
            }
        }
    }

    printf("\nAfter re-arranging even and odd elements ...\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    return 0;
}

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

After re-arranging even and odd elements …
10
2
8
4
6
5
7
3
9
1

Logic To Re-arrange Even and Odd Elements of An Array

First we accept N integer numbers from the user and store it inside a[N]. Using “for loop” we iterate through the array elements one by one.

Inside For Loop
We initialize i to 0, which is the first index of any array. We iterate through this “for loop” until i is less than or equal to j. j represents the number of elements already sorted from bottom(N – 1) of the array. i.e., from index j to (N – 1) all the elements are already odd numbers. For each iteration of the for loop we increment the value of i by 1.

First if condition – inside for loop
Aim of our C program is to move all the even elements to top and odd elements to the bottom. So if the “for loop” selected element, which is present in a[i] already has a even number then we let that number stay there itself, and increment the value of i by one. If the “for loop” selected number, which is present in a[i] has odd number, then we start searching for a even number from the bottom of the same array to swap it with a[i].

Inside While loop
Assume that a[i] has a odd number. Now lets initialize j to the size of array, which is N. “While loop” executes until j is greater than i. Here i represents the number of elements already sorted from the top. i.e., from index 0 to i all the elements are already even numbers.

This “while loop” executes until j is greater than i. The value of i is set by “for loop”. It’s the index of the element which is selected by the “for loop”, which has ODD number. i.e., if the control has entered “while loop” that means a[i] has ODD number. “While Loop” checks for EVEN element from the bottom of the array to swap it with the ODD number present at a[i].

Second if condition – inside while loop
As soon as control enters the while loop we reduce the value of j by 1. So now j is (N – 1), which is the last element of any array. Next, if the “while loop” selected element, which is present at a[j] has EVEN number, then we swap that number with the ODD number present in a[i], and break out of the while loop.

For each iteration of “while loop” we decrement the value of j by 1. And we do not reset the value of j for each iteration of “for loop”.

Printing re-arranged array elements
Once control exits “for loop” we print the array elements from 0 to N -1 and it’ll have all EVEN numbers at the top and ODD numbers at the bottom.

Explanation With Example

If int a[5] = {11, 10, 13, 12, 15};
ODD: a[i] % 2 != 0
EVEN: a[j] % 2 == 0

    j = N;
    for(i = 0; i <= j; i++)
    {
        if(a[i] % 2 != 0)
        {
            while(j > i)
            {
                j--;
                if(a[j] % 2 == 0)
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                    break;
                }
            }
        }
    }
ia[i]ODDja[j]EVENSwap
011TRUE415FALSENO
011TRUE312TRUEYES
110FALSE213FALSENO

As you can see from above table swapping occurs at a[0] and a[3]. a[0] has integer number 11 and a[3] has integer number 12. So after swapping these numbers a[5] will be: {12, 10, 13, 11, 15}; So a[0], a[1] has EVEN numbers and a[2], a[3], a[4] has ODD numbers.

Important Note: i always represents the number of elements sorted from top(EVEN numbers), and j represents the index from (N – 1) which are sorted from bottom(ODD numbers).

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 Divide/Split An Array Into Two At Specified Position

Lets write a C program to split or divide an array into two arrays at specified position.

Example: Expected Input/Output

Enter 10 integer numbers
-5
-4
-3
-2
-1
0
1
2
3
4
Enter position to split the array in to Two
4

Elements of First Array -> arr1[4]
-5
-4
-3
-2

Elements of Second Array -> arr2[6]
-1
0
1
2
3
4

Visual Representation

Split array at specified position

Video Tutorial: C Program To Divide/Split An Array Into Two At Specified Position


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

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

Source Code: C Program To Divide/Split An Array Into Two At Specified Position

#include<stdio.h>

#define N 10

int main()
{
    int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;

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

    printf("Enter position to split the array in to Two\n");
    scanf("%d", &pos);

    for(i = 0; i < N; i++)
    {
        if(i < pos)
            arr1[k1++] = a[i];
        else
            arr2[k2++] = a[i];
    }

    printf("\nElements of First Array -> arr1[%d]\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\n", arr1[i]);

    printf("\nElements of Second Array -> arr2[%d]\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\n", arr2[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
1
Enter position to split the array in to Two
3

Elements of First Array -> arr1[3]
1
2
3

Elements of Second Array -> arr2[7]
4
5
6
7
8
9
1

Logic To Divide/Split An Array Into Two At Specified Position

We accept N integer numbers from the user and store it inside array variable a[N]. Now we ask the user to input the position at which we need to split the array and create two separate arrays. Next, we iterate through array elements of a, using for loop(loop counter variable i is initialized to 0 and for loop executes until i < N and for each iteration of for loop i value increments by 1) and transfer all the elements of array a to array variable arr1 until i is less than user input position. Once i is greater than or equal to user input position, we transfer elements of a to array variable arr2.

Note: We’ve initialized k1 and k2 to 0, as index starts from 0. Whenever we push / insert values inside arr1 and arr2, we increment the index value of k1 and k2 respectively.

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 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