A program initialize a 2-D array, count the even elements and calculate their sum.

 

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j,b=0,c=0;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"Enter a number:";
cin>>a[i][j];
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]%2==0)
{
b++;
c=a[i][j]+c;
}
}
}

cout<<"There are "<<b<<" even numbers in the array."<<endl;
cout<<"The sum of these numbers is "<<c<<".";

getch();
}

 

< Back