C++ Program to display the diagonal elements of a given matrix [DEVCPP/GCC]

PROGRAM

//Program to display the diagonal elements of a given matrix

#include <iostream>
#define N 3

using namespace std;

int main()
{
        int i,j,Matrix[N][N];

        cout<<"\nENTER MATRIX\n";
        for(i=0 ; i<N ; i++)
        {
                for(j=0 ; j<N ; j++)
                {
                        cout<<"ENTER ["<<i<<"]["<<j<<"] ELEMENT: ";
                        cin>>Matrix[i][j];
                 }
        }

        cout<<"\nGIVEN MATRIX:\n";
        for(i=0 ; i<N ; i++)
        {
                for(j=0 ; j<N ; j++)
                {
                         cout<<Matrix[i][j]<<"  ";
                }
                cout<<endl;
        }

        cout<<"\nDIAGONAL ELEMENTS\n";

        cout<<"\nFIRST DIAGONAL: ";
        for(i=0 ; i<N ; i++)
        {
                 cout<<Matrix[i][i]<<"  ";
        }

        cout<<"\nSECOND DIAGONAL: ";
        for(i=0,j=N-1 ; i<N ; i++,j--)
        {
                cout<<Matrix[i][j]<<"  ";
        }

         return 0;
}

OUTPUT

C++ Program to display the diagonal elements of a given matrix with output

Share this

Related Posts

3 comments

comments
Anonymous
August 13, 2017 at 11:25 AM delete

I am unable to solve output questions please provide me some tips and techniques to get it done easily

Reply
avatar
August 13, 2017 at 1:35 PM delete

Hi Shashank, Post your programming related queries here: https://goo.gl/forms/jqW2yaG7YU

Reply
avatar
November 12, 2018 at 6:24 PM delete

thank you for sharing useful post.
c++ programming tutorial
welookups

Reply
avatar

FIND US ON FACEBOOK!