C++ Program to Print Corner Elements of Matrix

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Print corner elements
#include<iostream>
#define clrscr() system("cls")
using namespace std;

int main()
{
      int a[50][50],r,c,m,n;
      clrscr();
      xyz: cout<<"\nEnter the  value row and column\n";
      cin>>m>>n;
      if(m>50 || n>50)
      {
         cout<<"\n Invalid limit\n";
          goto xyz;
      }
      //Read matrix
      cout<<"\nEnter elements in matrix";
      for(r=0;r<m;r++)
      {
        for(c=0;c<n;c++)
          cin>>a[r][c];
      }

    for(r=0;r<m;r++)
      {
        for(c=0;c<n;c++)
        {
          cout<<a[r][c]<<"\t";
        }  
        cout<<"\n";
      }
      // Corner elements
   for(r=0;r<m;r++)
   {
      for(c=0;c<n;c++)
     {
          if(r==0 && c==0)
            cout<<"\nTop left element is: "<<a[r][c]<<endl;
          if(r==0 && c==n-1)
            cout<<"\nTop right element is: "<<a[r][c]<<endl;
         if(r==m-1 && c==0)
            cout<<"\nBottom left element is: "<<a[r][c]<<endl;
        if(r==m-1 && c==n-1)
            cout<<"\nBottom right element is: "<<a[r][c]<<endl;
     }
   }


}

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *