Coders Packet

C++ Program to print a matrix diagonally

By SHAHINA S

This will discuss how to print a matrix diagonally using C++ Program. An example is given for printing a matrix diagonally.

C++ Program to print a matrix diagonally.

        1    2    3

        4    5    6

        7    8    9

1

4    2

7    5    3

8    6

9

Given below is the code for how to print a matrix diagonally using C++.

#include
using namespace std;
int main()
{
int m,n,M[50][50],i,j;
cout<<"Enter how many rows and columns in a matrix: \n";
cin>>m>>n;
cout<<"Enter elements into the matrix: ";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>M[i][j];
}
}
cout<<"\n\n\t";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<M[i][j]<<" ";
}
cout<<endl<<"\t";
}
cout<<endl;
for(int p=0;p<m;p++)
{
i=p;
j=0;
cout<<"\t";
while(i>=0)
{
cout<<M[i][j];
i=i-1;
j=j+1;
cout<<" ";
}
cout<<endl;
}
for(int p=1;p<n;p++)
{
i=m-1;
j=p;
cout<<"\t";
while(j<n)
{
cout<<M[i][j];
i=i-1;
j=j+1;
cout<<" ";
}
cout<<endl;
}
}

 

Download project

Reviews Report

Submitted by SHAHINA S (Shahina123)

Download packets of source code on Coders Packet