programming-examples/c-sharp/Arrays/C# Sharp to find sum of right diagonals of a matrix.cs
2019-11-15 12:59:38 +01:00

32 lines
1.1 KiB
C#

using System;
public class Exercise23
{
public static void Main()
{
int i,j,sum=0,n;
int[,] arr1 = new int[50,50];
Console.Write("\n\nFind sum of right diagonals of a matrix :\n");
Console.Write("---------------------------------------\n");
Console.Write("Input the size of the square matrix : ");
n=Convert.ToInt32(Console.ReadLine());
Console.Write("Input elements in the first matrix :\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
Console.Write("element - [{0}],[{1}] : ",i,j);
arr1[i,j]=Convert.ToInt32(Console.ReadLine());
if (i==j) sum= sum+arr1[i,j];
}
}
Console.Write("The matrix is :\n");
for(i=0; i<n; i++)
{
for(j=0; j<n ; j++)
Console.Write("{0} ",arr1[i,j]);
Console.Write("\n");
}
Console.Write("Addition of the right Diagonal elements is :{0}\n",sum);
}
}