Multiply Diagonal Elements of Matrix

Code Snippets 4 U
using System;

namespace DiagnolMultiplication
{
    internal class DiagnoalMultiplier
    {
        /// <summary>
        /// Below function multiplies 5×5 matrixs' diagnol elements
        /// and print resultant
        /// </summary>
        internal void Multiply()
        {
            int matrixSize = 5;
            int[,] matrixOne =  {
                                {1,2,3,4,5 },
                                {6,7,8,9,10},
                                {11,12,13,14,15},
                                {16,17,18,19,20},
                                {21,22,23,24,25}};

            int[,] matrixTwo =  {
                                {10,20,30,40,50 },
                                {60,70,80,90,100},
                                {1,2,3,4,5},
                                {6,7,8,9,0},
                                {10,2,213,4,5}};

            int[,] matrixResult = new int[matrixSize, matrixSize];

            

            // multiply diagnol elements
            for (int i = 0; i < matrixSize; i++)
            {
                for (int j = 0; j < matrixSize; j++)
                {
                    if (i == j)
                    {
                        matrixResult[i, j] = matrixOne[i, j] * matrixTwo[i, j];
                    }

                    else if ((i + j) == matrixSize-1)
                    {
                        matrixResult[i, j] = matrixOne[i, j] * matrixTwo[i, j];
                    }

                }
            }

            // show the result matrix

            for (int i = 0; i < matrixSize; i++)
            {
                for (int j = 0; j < matrixSize; j++)
                {
                    if (i == j || i + j == matrixSize-1)
                        Console.Write("{0}", matrixResult[i, j]);
                    else
                        Console.Write("   ");
                }
                Console.WriteLine();
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

+ fifteen = twenty