Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

3 Apr 2013

COUNT TOTAL NUMBER OF EVEN AND ODD NUMBERS IN A MATRIX AND PRINT THEM IN C++

Write a program to count total number of even and odd elements in a matrix and print that elements


#include<iostream.h>
#include<conio.h>
void main()
{   clrscr();
 int a[10][10];
 int i, j, n, m;
 int x[100],y[100],even=0,odd=0;
 cout<<"enter no of rows\n";
 cin>>n;
 cout<<"enter no of colomn\n";
 cin>>m;
 cout<<"enter the elements of matrix\n";
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"a["<<i+1<<"]["<<j+1<<"]:= ";
     cin>>a[i][j];
    }
  }
 cout<<"matrix u have entered is"<<endl;
 for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     cout<<"\t"<<a[i][j];
    }
    cout<<endl;
  }
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
     if(a[i][j]%2==0 && a[i][j]>=0)
      {
       x[even]=a[i][j];
       even++;
      }
     else
      if(a[i][j]%2!=0 && a[i][j]>=0)
       {
        y[odd]=a[i][j];
        odd++;
       }
    }
  }
  cout<<"total even elements are= "<<even<<endl;
  cout<<"even elements are"<<endl;
  for(i=0;i<even;i++)
   {
    cout<<x[i]<<endl;
   }
  cout<<"total odd elements are= "<<odd<<endl;
  cout<<"odd elements are"<<endl;
  for(i=0;i<odd;i++)
   {
    cout<<y[i]<<endl;
   }
getch();
}

==OUTPUT==

enter no of rows
2
enter no of column
2
enter the elements of matrix
a[1][1]:=1
a[1][2]:=2
a[2[1]:=3
a[2][2]:=4
matrix you have entered is
1  2
3  4
total even elements are=2
even elements are
2
4
total odd elements are=2
odd elements are
1
3

No comments:

Post a Comment