Advertisement
marekov

SumAboveMainDIagonal

May 31st, 2020
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. namespace MatrixSum
  2. {
  3.     using System;
  4.     using System.Numerics;
  5.  
  6.     public class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             var rows = int.Parse(Console.ReadLine());
  11.             var matrix = GetMatrix(rows);
  12.             BigInteger sum = 0;
  13.  
  14.             for (int row = 0; row < matrix.GetLength(0); row++)
  15.             {
  16.                 for (int col = 0; col < matrix.GetLength(1); col++)
  17.                 {
  18.                     if (col > row)
  19.                     {
  20.                         sum += matrix[row, col];
  21.                     }
  22.                 }
  23.             }
  24.             Console.WriteLine(sum);
  25.  
  26.         }
  27.         static int[,] GetMatrix(int size)
  28.         {
  29.             var matrix = new int[size, size];
  30.  
  31.             var currentRow = 1;
  32.             var counter = 1;
  33.             for (int row = 0; row < size; row++)
  34.             {
  35.                 for (int col = 0; col < size; col++)
  36.                 {
  37.                     matrix[row, col] = counter;
  38.                     counter *= 2;
  39.  
  40.                     if (col == size - 1)
  41.                     {
  42.                         currentRow *= 2;
  43.                         counter = currentRow;
  44.                     }
  45.                 }
  46.             }
  47.             return matrix;
  48.         }
  49.       /*  static void PrintMatrix(int rows, int[,] matrix)
  50.         {
  51.             for (int row = 0; row < rows; row++)
  52.             {
  53.                 for (int col = 0; col < rows; col++)
  54.                 {
  55.                     Console.Write(matrix[row, col] + " ");
  56.                 }
  57.                 Console.WriteLine();
  58.             }
  59.         }*/
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement