Count Zero and Non Zero


Explanation

A two-dimensional array stores data in the form of rows and columns. We can perform different operations on 2D arrays, such as counting specific values present inside the array.

In this program, a 4 x 4 two-dimensional array is created. The user enters values into the array. The program checks each element of the array using the if-else statement. If the value is 0, it increases the zero count; otherwise, it increases the non-zero count.

How It Works?

  • Step 1: Declare a two-dimensional array m[4][4] and initialize variables z and nz with 0.
  • Step 2: Use nested for loops to accept values from the user.
  • Step 3: Display all elements of the array using nested loops.
  • Step 4: Check each array element using if condition.
  • Step 5: If the element is equal to zero, increase the value of z.
  • Step 6: Otherwise, increase the value of nz.
  • Step 7: Display the total number of zero and non-zero elements.

Example Program


#include <stdio.h>
#include <conio.h>

main()
{

    int m[4][4], k, p, z = 0, nz = 0;

    clrscr();

    for(k = 0; k <= 3; k = k + 1)
    {

        for(p = 0; p <= 3; p = p + 1)
        {

            printf("Enter no : ");

            scanf("%d", &m[k][p]);

        }

    }


    for(k = 0; k <= 3; k = k + 1)
    {

        for(p = 0; p <= 3; p = p + 1)
        {

            printf("%d ", m[k][p]);

            if(m[k][p] == 0)
            {

                z++;

            }

            else
            {

                nz++;

            }

        }

        printf("\n");

    }


    printf("\nZero %d", z);

    printf("\nNon Zero %d", nz);


    getch();

}

Output

Enter no : 1
Enter no : 0
Enter no : 5
Enter no : 0
Enter no : 8
Enter no : 9
Enter no : 0
Enter no : 3
Enter no : 6
Enter no : 2
Enter no : 0
Enter no : 7
Enter no : 4
Enter no : 1
Enter no : 0
Enter no : 9

1 0 5 0
8 9 0 3
6 2 0 7
4 1 0 9

Zero 5
Non Zero 11

Practice Exercises

Task 1: Write a C program to create a 3 x 3 two-dimensional array and count the total number of even and odd elements present in the array.

Goal: Learn how to perform calculations on 2D array elements using nested for loops and if-else conditions.

💡 Hint: Declare a 2D array using int m[3][3]. Initialize two variables e and o with 0. If m[k][p] % 2 == 0, increase the even count; otherwise, increase the odd count.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{

    int m[3][3], k, p, e = 0, o = 0;

    clrscr();

    for(k = 0; k <= 2; k++)
    {

        for(p = 0; p <= 2; p++)
        {

            printf("Enter no : ");

            scanf("%d", &m[k][p]);

        }

    }


    for(k = 0; k <= 2; k++)
    {

        for(p = 0; p <= 2; p++)
        {

            printf("%d ", m[k][p]);

            if(m[k][p] % 2 == 0)
            {

                e++;

            }

            else
            {

                o++;

            }

        }

        printf("\n");

    }


    printf("\nEven Total %d", e);

    printf("\nOdd Total %d", o);


    getch();

}

Output
Enter no : 10
Enter no : 5
Enter no : 8
Enter no : 3
Enter no : 12
Enter no : 7
Enter no : 6
Enter no : 9
Enter no : 4

10 5 8
3 12 7
6 9 4

Even Total 5
Odd Total 4
Explanation: The program accepts values in a 3 x 3 two-dimensional array. Each element is checked using the modulus operator. If the remainder is zero, the value is counted as even; otherwise, it is counted as odd. Finally, the total number of even and odd elements is displayed.