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

C Program To Find Size of An Array

Lets write c program to find number of elements present in an array, where the array size is not mentioned explicitly.

Related Read:
Sizeof Operator in C Programming Language

Example: Expected Output

int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
We have 25 no of elements in array.

Video Tutorial: C Program To Find Number of Elements In An Array


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

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

Source Code: C Program To Find Size of An Array

Method 1

#include<stdio.h>

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    int count = sizeof(a) / sizeof(int);

    printf("We have %d no of elements in array\n", count);

    return 0;
}

Output:
We have 25 no of elements in array.

We know that array variable name contains base address or the address of first array element. But sizeof(array_name) gives back the number of bytes occupied by the entire array. Now divide this value by the memory occupied by 1 element – this can be determined either by using sizeof() on the data type(as shown in above source code) or any of the element in the array(as shown in below source code).

Method 1

#include<stdio.h>

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    int count = sizeof(a) / sizeof(a[0]);

    printf("We have %d no of elements in array\n", count);

    return 0;
}

Output:
We have 25 no of elements in array.

We assume that the array will at least have 1 element, so we find the number of bytes occupied by it. This is the best approach because, this way we’ll not be compelled to change the data type passed to sizeof() operator whenever we need to check the number of elements present in a array of different data type.

Method 3: Using Macros

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};

    printf("We have %d no of elements in array.\n", SIZE(a));

    return 0;
}

Output:
We have 25 no of elements in array.

Here we pass the array variable name to macro SIZE(). Again the macro expansion will be replacing the macro template before passing the source code to the compiler.

Related Read:
Macros With Arguments: C Program

Using this method has advantage too. We can calculate size of multiple array variables, of different data type: as shown in below example.

Method 4: Using Macros

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
    char ch[] = {'i', 'P', 'h', 'o', 'n', 'e'};
    double dbl[] = {2.3, 4, 5.2, 6.3, 7.1, 3.4, 1.3};

    printf("Integer variable a[%d].\n", SIZE(a));
    printf("Character variable ch[%d].\n", SIZE(ch));
    printf("Double variable dbl[%d].\n", SIZE(dbl));

    return 0;
}

Output:
Integer variable a[25].
Character variable ch[6].
Double variable dbl[7].

Here we find the number of elements present in array variable a and ch using the same macro SIZE.

Where Is It useful?

Whenever we want to iterate through individual elements of the array and we’ve no idea whats the size of the array or whats the largest designator of that array. In such cases we can’t write condition inside loop.

Using above formula we could easily find the number of elements present in the array and make use of the result in our for loop condition:

#include<stdio.h>

#define SIZE(a) ( sizeof(a) / sizeof(a[0]) )

int main()
{
    int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23,
               56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
    int count = SIZE(a);

    for(i = 0; i < count; i++)
        printf("%d\n", a[i]);

    return 0;
}

This prints all the elements of the array.

Note: Do not write i < SIZE(a) condition directly inside for loop. This would create unnecessory overhead, as for each iteration SIZE(a) [which will be replaced by ( sizeof(a) / sizeof(a[0]) ) while preprocessing] has to calculate the size of the array. Instead calculate the size of array once and store it inside a variable and then use that variable in your for loop as shown in above source code.

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