Neeraj Mishra

A crazy computer and programming lover. He spend most of his time in programming, blogging and helping other programming geeks.

C++ Program to Find Sum of Elements Above and Below Main Diagonal of Matrix

Here is the C++ program to find sum of elements above and below the main diagonal of square matrix. #include<iostream> using namespace std; int main() { int arr[5][5],a=0,b=0,i,j,n; cout<<“Enter size of matrix(max 5):”; cin>>n; cout<<“Enter the matrix:\n”; for(i=0;i<n;++i) for(j=0;j<n;++j) cin>>arr[i][j]; for(i=0;i<n;++i) for(j=0;j<n;++j) if(j>i) a+=arr[i][j]; else if(i>j) b+=arr[i][j]; cout<<“\nSum of elements above the diagonal:”<<a; cout<<“\nSum of …

C++ Program to Find Sum of Elements Above and Below Main Diagonal of Matrix Read More »

Stack in C++ Using Linked List

Here you will learn about stack in C++ using a program example. Stack is an abstract datatype which follows Last In First Out (LIFO) principle. Basically stack has two operations namely push and pop.  push: inserting an element at the top of the stack pop: removing an element from the top of the stack Below …

Stack in C++ Using Linked List Read More »

C++ Program to Print Upperhalf and Lowerhalf Triangle of Square Matrix

Here is the C++ program to print upperhalf and lowerhalf triangle of a square matrix. #include<iostream> using namespace std; int main() { int a[10][10],i,j,m; cout<<“Enter size of the Matrix(min:3,max:5):”; cin>>m; cout<<“\nEnter the Matrix row wise:\n”; for(i=0;i<m;i++) for(j=0;j<m;++j) cin>>a[i][j]; cout<<“\n\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<<“\n\n”; for(i=0;i<m;++i) { …

C++ Program to Print Upperhalf and Lowerhalf Triangle of Square Matrix Read More »

C++ Program to Find Largest and Second Largest Number in 2D Array

Here is the C++ program to find largest and second largest number in a 2d array or matrix. #include<iostream> using namespace std; int main() { int a[5][5],big1=1,big2=0,n,m,i,j; cout<<“Enter no of rows and columns(max 5):”; cin>>m>>n; cout<<“Enter the array:\n”; 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(a[i][j]>big1) big1=a[i][j]; } for(i=0;i<m;++i) for(j=0;j<n;++j) { if(a[i][j]>big2&&a[i][j]<big1) big2=a[i][j]; } cout<<“\nLargest number:”<<big1; …

C++ Program to Find Largest and Second Largest Number in 2D Array Read More »

C++ Program to Find Sum of Diagonals of Matrix

Here is the C++ program to find the sum of diagonals of a matrix. The matrix should be a square matrix. #include<iostream> using namespace std; int main() { int a[5][5],d1sum=0,d2sum=0,m,i,j; cout<<“Enter size of the square matrix(max 5):”; cin>>m; cout<<“\nEnter the Matrix row wise:\n”; for(i=0;i<m;i++) for(j=0;j<m;++j) cin>>a[i][j]; for(i=0;i<m;++i) for(j=0;j<m;++j) { if(i==j) d1sum+=a[i][j]; if(i+j==(m-1)) d2sum+=a[i][j]; } cout<<“\nSum …

C++ Program to Find Sum of Diagonals of Matrix Read More »