using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
public class MatrixDiagonal
{
public static void Main(string[] args)
{
Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");
int[] RowsCols = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
int Rows = RowsCols [0];
int columns = RowsCols [1];
int[,] UserMatrix = new int[Rows, columns];
int j = 0;
Console.WriteLine("Enter Matrix Elements in Matrix Form separated by single Space");
for (int i = 0; i < Rows; i++)
{
int[] Elements= Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
foreach (var e in Elements)
{
UserMatrix[i, j] = e;
j++;
}
j = 0;
}
int LeftDiagonal = 0;
int RightDiagonal = 0;
int y = columns - 1;
for (int r = 0; r < Rows; r++)
{
LeftDiagonal = LeftDiagonal + UserMatrix[r, r];
RightDiagonal = RightDiagonal + UserMatrix[r, y];
y--;
}
Console.WriteLine("Sum of Left Diagonal Elements : {0}", LeftDiagonal);
Console.WriteLine("Sum of Right Diagonal Elements : {0}", RightDiagonal);
if (LeftDiagonal == RightDiagonal)
Console.WriteLine("Both Diagonals are Equal");
else if (LeftDiagonal > RightDiagonal)
Console.WriteLine("LeftDiagonal is greater than RightDiagonal");
else
Console.WriteLine("LeftDiagonal is Less than RightDiagonal");
Console.ReadLine();
}
}
}
Output 1 :
Output 2 :
Output 3 :
No comments:
Post a Comment