Social Icons

Sunday, 16 February 2014

Program to print upper-half and lower-half of a square Matrix

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[5][5],i,j,m;
cout<<" Ente size of the Matrix : ";
cin>>m;
cout<<"\n Enter elements : ";

for(i=0;i<m;i++)
for(j=0;j<m;++j)
cin>>a[i][j];

cout<<" Your matrix is \n";

for(i=0;i<m;i++)
{
cout<<" \n ";
for(j=0;j<m;++j)
cout<<a[i][j]<<"  ";
}

cout<<"\n\n";

cout<<" Upper-Half is\n";
for(i=0;i<m;++i)
{
for(j=0;j<m;++j)
{
if(i<j)
cout<<a[i][j]<<" ";
else
cout<<"  ";
}
cout<<"\n";
}

cout<<" Lower-Half is";
for(i=0;i<m;++i)
{
cout<<" ";
for(j=0;j<m;++j)
{
if(j<i)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout<<"\n";
}

getch();
}


                                                        Output

 

No comments:

Post a Comment