C Program To Count Positive, Negative, Even And Odd Numbers In An Array

Twenty-five numbers are entered from the keyboard into an array. Write a program to find out how many of them are positive, how many are negative, how many are even and how many odd.

Important Note
Most of the time 0 is considered as even number, as it has odd numbers on either side of it i.e., 1 and -1. And 0 is perfectly divisible by 2.

Also note that 0 is neither positive and nor negative.

number scale

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

Example: Expected Output

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

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Visual Representation

count positive negative even and odd elements in array

Video Tutorial: C Program To Count Positive, Negative, Even And Odd Numbers In An Array


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

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

Source Code: C Program To Count Positive, Negative, Even, Odd And Zero Numbers In An Array

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, pos = 0, neg = 0, 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] > 0)
            pos++;
        else
            neg++;

        if(a[i] == 0)
        {

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

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);
    printf("Zero: %d\n", zero);

    return 0;
}

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

Positive: 5
Negative: 4
Even: 4
Odd: 5
Zero: 1

Here we are counting positive numbers, negative numbers, even numbers, odd numbers and zeros.

Source Code: C Program To Count Positive, Negative, Even And Odd Numbers In An Array

#include<stdio.h>

#define N 10

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

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

        }
        else if(a[i] > 0)
            pos++;
        else
            neg++;

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

    printf("\nPositive: %d\n", pos);
    printf("Negative: %d\n", neg);
    printf("Even: %d\n", even);
    printf("Odd: %d\n", odd);

    return 0;
}

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

Positive: 5
Negative: 4
Even: 5
Odd: 5

In above source code, since 0 is perfectly divisible by 2, it is considered as even number.

Logic To Count Positive, Negative, Even And Odd Numbers In An Array

We ask the user to enter N integer numbers and store it inside array variable a[N]. Next we loop through the array using for loop. For each iteration we check: if the selected number is greater than 0. If true, then its positive number, so we increment the value of pos by 1. If the number is less than 0, then its a negative number, so we increment the value of neg by 1.

Next we check if the selected number is perfectly divisible by 2. If true, then its even number, so we increment the value of variable even by 1. If the selected number is not perfectly divisible by 2, then its odd number.

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 Segregate 0s and 1s In An Array using Swapping Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using Swapping method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Counting Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
1
0
1
0
1
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
0
0
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Swapping Method


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

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Swapping Method

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, left = 0, right = N - 1;

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

        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0 and 1 as input\n");
            i--;
        }
    }

    while(left < right)
    {
        while(a[left]   == 0)
            left++;

        while(a[right] == 1)
            right--;

        if(left < right)
        {
            a[left++] = 0;
            a[right--] = 1;
        }
    }

    printf("\nArray after sorting 0's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
1
0
0
1
0

Array after sorting 0’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Swapping Method

We ask the user to input N integer numbers and store it inside a[N]. We assign 0(the first index of any array) to variable left and N – 1(the last index of any array) to variable right.

The outer while loop executes until left < right. Inside we write another while loop to check from left, if a[left] is 0. If its already 0, then its already in sorted order, so we don’t swap it. We simply increment the value of left by one position. Control exits this first inner while loop when a[left] is not equal to 0.

Similarly, we check if a[right] is 1. If its 1, we decrement the value of right by 1. When a[right] is not equal to 1, the control exits.

Now variable “left” will have the index position from left where there is number 1. And variable “right” will have the index position from right where there is number 0. Now we need to swap it.

We check if value of left is still less than the value of right. If true, we store 0 at a[left] and 1 at a[right].

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

lefta[left]righta[right]Swap
0041NO
1130YES
2121NO

a[5] after sorting 0’s to left and 1’s to right:
a[5] = {0, 0, 1, 1, 1};

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 Segregate 0s and 1s In An Array using Counting Method

Write a C program to segregate 0’s to the left and 1’s to the right of an array using counting method.

Related Read:
C Program To Segregate 0s and 1s In An Array using Swapping Method

Example: Expected Input/Output

Enter 10 elements(0 or 1)
1
0
0
1
1
1
1
0
0
1

Array after segregating o’s to left and 1’s to right
0
0
0
0
1
1
1
1
1
1

Visual Representation

Segregate 0s and 1s In An Array

Video Tutorial: C Program To Segregate 0’s and 1’s In An Array using Counting Method


[youtube https://www.youtube.com/watch?v=gHW6WI-5724]

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

Source Code: C Program To Segregate 0’s and 1’s In An Array using Counting Method

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i, count = 0;

    printf("Enter %d elements(0 or 1)\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);
        if(a[i] != 0 && a[i] != 1)
        {
            printf("Please enter only 0's and 1's as input\n");
            i--;
        }
        else if(a[i] == 0)
            count++;
    }

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

    printf("\nArray after segregating o's to left and 1's to right\n");
    for(i = 0; i < N; i++)
        printf("%d\n", a[i]);

    printf("\n");

    return 0;
}

Output 1:
Enter 5 elements(0 or 1)
0
1
1
0
0

Array after segregating o’s to left and 1’s to right
0
0
0
1
1

Output 2:
Enter 5 elements(0 or 1)
2
Please enter only 0’s and 1’s as input
10
Please enter only 0’s and 1’s as input
1
0
2
Please enter only 0’s and 1’s as input
1
1
0

Array after segregating o’s to left and 1’s to right
0
0
1
1
1

Logic To Segregate 0’s and 1’s In An Array using Counting Method

We ask the user to input N integer numbers and store it inside a[N]. Next we iterate through each element of the array and if the user input is 0, then we increment the value of variable count by 1. After completing the iterations, variable count will have the number of 0’s input by the user.

Modifying the Array
Since user is allowed to either enter 0 or 1. So if we know the number of 0’s input by the user, we can easily calculate the number of 1’s in the array, using the formula:

N – count_0 = count_1
where, N is array size.

Now that we know the exact number of 0’s and 1’s input by the user, we re-enter the 0’s and 1’s as needed by the problem statement. That is, we arrange all the 0’s to left and 1’s to the right.

Number of 0’s plus the number of 1’s should be equal to the array size.
count_0 + count_1 = N;

Explanation With Example

If a[5] = {0, 1, 1, 0, 1};

ia[i]count
001
11
21
302
41

count = 2;
That is, the number of 0’s in user input array is 2. So the number of 1’s is:
array_size – count
5 – 2 = 3
So there are three 1’s and two 0’s.

    for(i = 0; i < N; i++)
    {
        if(i < count)
            a[i] = 0;
        else
            a[i] = 1;
    }

Once we have this exact data. We can arrange Two 0’s from left of array, i.e., a[0] and a[1]. And three 1’s from a[2], a[3] and a[4].

This is how we segregate 0’s to left and 1’s to the right of an array, using counting method.

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 and Display Even and Odd Elements of An Array

Lets write a C program to display even and odd elements of an array, along with their count.

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

How To Determine Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2.
Ex: num % 2 == 0

An odd number is an integer that is not exactly divisible by 2.
Ex: num % 2 != 0

Example: Expected Input/Output

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

Even numbers in the array are …
2
4
6
8
10

Odd numbers in the array are …
1
3
5
7
9

Total Even numbers: 5
Total Odd numbers: 5

Visual Representation

count and display even and odd elements of array

Video Tutorial: C program To Count and Display Even and Odd Elements of An Array


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

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

Source Code: C program To Count and Display Even and Odd Elements of An Array

#include<stdio.h>

#define N 10

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

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

    printf("\n\nEven numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }

    printf("\nOdd numbers in the array are ...\n");
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }

    printf("\n\nTotal Even numbers: %d", even);
    printf("\nTotal Odd numbers: %d\n", odd);

    printf("\n");

    return 0;
}

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

Even numbers in the array are …
10
12
14
16
18

Odd numbers in the array are …
11
13
15
17
19

Total Even numbers: 5
Total Odd numbers: 5

Output 2:
Enter 10 integer numbers
65
12
4
7
87
2
36
45
78
98

Even numbers in the array are …
12
4
2
36
78
98

Odd numbers in the array are …
65
7
87
45

Total Even numbers: 6
Total Odd numbers: 4

Logic To Count and Display Even and Odd Elements of An Array

We ask the user to enter N integer number and store it in array variable a[N]. We iterate or loop through the user input array elements one by one and check if the selected element is perfectly divisible by 2. If true, then its even number, so we display the number/element and keep count of number of even numbers.

Similarly, we loop through the user input array elements one by one once again, and check if the selected element is not perfectly divisible by 2. If true, then its odd number. So we display the odd number and keep the count of number of odd elements.

Once both the iterations are complete, we display the number of even and odd elements count which we kept track of in above two iterations.

Note: We’re considering 0 as even number, as it has odd elements on either side of it. And 0 is perfectly divisible by 2.
scale

Explanation With Example

If a[5] = {10, 11, 12, 13, 14};
For Even Numbers

    even = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
        {
            printf("%d\n", a[i]);
            even++;
        }
    }
ia[i]a[i] % 2 == 0ResultDisplayCount
0a[0] = 1010 % 2TRUE101
1a[1] = 1111 % 2FALSE
2a[2] = 1212 % 2TRUE122
3a[3] = 1313 % 2FALSE
4a[4] = 1414 % 2TRUE143

Even numbers displayed:
10
12
14
Count: 3

For Odd Numbers

    odd = 0;
    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 != 0)
        {
            printf("%d\n", a[i]);
            odd++;
        }
    }
ia[i]a[i] % 2 != 0ResultDisplayCount
0a[0] = 1010 % 2FALSE
1a[1] = 1111 % 2TRUE111
2a[2] = 1212 % 2FALSE
3a[3] = 1313 % 2TRUE132
4a[4] = 1414 % 2FALSE

Odd numbers displayed:
11
13
Count: 2

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