Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Matrixx
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random rand = new Random();
- int n = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, n];
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = rand.Next(0, 3);
- }
- }
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write("{0:D2} ", matrix[row, col]);
- }
- Console.WriteLine();
- }
- int sum = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (col == row)
- {
- sum += matrix[row, col];
- }
- }
- }
- Console.WriteLine("Primary diagonal sum = {0}", sum);
- sum = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if ((col + row) == n - 1)
- {
- sum += matrix[row, col];
- }
- }
- }
- Console.WriteLine("Secondary diagonal sum = {0}", sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement