Source code:
//Write a C code to count the number of negative elements in a 2D matrix.
#include<stdio.h>
int main()
{
int row, col, i, j, count=0;
int a[10][10];
printf("Enter the number of row & column:");
scanf("%d %d", &row, &col);
printf("Enter the matrix:\n");
for (i=0; i<row; ++i)
{
for (j=0;j<col; ++j)
scanf("%d",&a[i][j]);
}
for (i=0; i<row; ++i)
{
for (j=0;j<col; ++j)
{
if(a[i][j]<0)
count = count + 1;
}
}
printf("No. of Negative Elements = %d",count);
return 0;
}
Output:
No comments:
Post a Comment