Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 35: Write a C++ program to check whether the given matrix is tridiagonal or not.

#include<iostream>
using namespace std;

int main()
{
	int n,flag=0;
	cout<<"\nenter the total no of rows of square matrix : ";
	cin>>n;
	int arr[n][n];
	cout<<"\nenter a matrix :\n";

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>arr[i][j];
			if(((i-j)>1||(i-j)<-1)&&arr[i][j]!=0)
				flag=1;
		}
	}

	if(flag==0)
		cout<<"\nyes,it's a tridiagonal matrix!!";
	else
		cout<<"\nno,it's not a tridiagonal matrix!!";

	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.