0% found this document useful (0 votes)
4 views2 pages

Diagonal Matrix

The document contains two C++ programs that check if a given square matrix is a diagonal matrix. The first program prompts the user for matrix dimensions and elements, while the second program specifically handles a 3x3 matrix. Both programs verify the diagonal condition and output the result accordingly.

Uploaded by

bscs22f22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Diagonal Matrix

The document contains two C++ programs that check if a given square matrix is a diagonal matrix. The first program prompts the user for matrix dimensions and elements, while the second program specifically handles a 3x3 matrix. Both programs verify the diagonal condition and output the result accordingly.

Uploaded by

bscs22f22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*#include<iostream>

using namespace std;


int main ()
{
int i, j, m, n, check = 0, A[10][10];
cout << "Enter number of rows and columns : ";
cin >> m >> n;
if (m != n)
{
cout << "Matrix is not a square matrix!";
exit(0);
}
cout << "Enter elements of matrix : ";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (i == j)
{
if (A[i][j] == 0)
check = 1;
}
else
{
if (A[i][j] != 0)
check = 1;
}
if (check == 1)
cout << " Array is not a diagonal matrix.";
else
{
cout << " Array is a diagonal matrix.\n ";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout << A[i][j] << " ";
cout << "\n ";
}
}
return 0;
}*/

#include<iostream>
using namespace std;
int main( )
{
int s[3][3];
int i, j, check;
cout<<"2D Array Input:"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"["<<i<<"]["<<j<<"]= "<<endl;
cin>>s[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j){
if (s[i][j]==0){
check =1;}}
else
{
if (s[i][j]!=0){
check=1;}}
} }

if (check=1){
cout<<" Array is not diagonal"<< endl;}

cout<<"The 2-D Array is:"<< endl;


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<s[i][j];
}
cout<<endl;
}
return 0;
}

You might also like