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?
m[4][4] and initialize variables z and nz with 0.for loops to accept values from the user.if condition.z.nz.#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(); }
for loops and if-else conditions.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.#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(); }