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 »

Linear Search in C++

Here you will get program for linear search in C++. In linear search algorithm, we compare targeted element with each element of the array. If the element is found then its position is displayed. The worst case time complexity for linear search is O(n). Program for Linear Search in C++ #include<iostream> using namespace std; int main() …

Linear Search in C++ Read More »

C++ Program to print following triangle:

         *       *  *      *    *     ***** code: #include<iostream.h>#include<conio.h> void main(){ clrscr(); int i,k,j,n; cout<<“Enter size of triangle in odd number(min-5,ex-5,7,9):”; cin>>n; cout<<“nt”; for(i=1;i<=n;i+=2) { for(j=n;j>i;j-=2) cout<<” “; for(k=1;k<=i;++k) { if(i==n) cout<<“*”; else if(k==1||k==i) cout<<“*”; else cout<<” “; } cout<<“nt”; } getch();}

C++ Program to print following triangle:

      *     **   *** ****code: #include<iostream.h>#include<conio.h> void main(){ clrscr(); int i,n,j,k; cout<<“Enter size of triangle(ex-5):”; cin>>n; cout<<“nt”; for(i=0;i<n;++i) { for(j=n;j>i;–j) cout<<” “; for(k=0;k<+i;++k) cout<<“*”; cout<<“nt”; } getch();}