Using System;
class Program
{
static void Main()
{
var random = new Random(DateTime.Now.Millisecond);
var n = random.Next(5, 10);
var matrix = new int[n, n];
for (var i = 0; i < matrix.GetLength(0); i++)
{
for (var j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = random.Next(0, 9);
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("Sum = {0}", SumAboveMainDiagonal(matrix));
Console.ReadKey();
}
static int SumAboveMainDiagonal(int[,] m)
{
var sum = 0;
Console.WriteLine("Summing elements:");
for (var i = 0; i < m.GetLength(0); i++)
{
for (var j = 0; j < m.GetLength(1); j++)
{
if (i < j)
{
sum += m[i, j];
Console.Write(m[i, j] + " ");
}
else { Console.Write(0 + " "); }
}
Console.WriteLine();
}
return sum;
}
}