write a program to add the diagonal of two-dimensional array

write a program to add the diagonal of two-dimensional array.
Answer
Diagonal are the position where value of x axis and y axis cordinates are same. For example. Programming Example
using System;

namespace Add_Diagonal
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int[,] num= {
                {22,50,11, 2, 49},
                {92,63,12,64,37},
                {75,23,64,12,99},
                {21,25,71,69,39},
                {19,39,58,28,83}};

                //Getting Row and Column Length
                int rowlength = num.GetLength(0);
                int columnlength = num.GetLength(1);
                int total=0;
                Console.Write("Diagonals are : ");
                for(int row=0; row<rowlength; row++)
                {
                    for(int column=0; column<columnlength; column++)
                    {
                        if(row==column)
                        {
                            Console.Write(num[row,column] + " ");
                            total += num[row,column];
                        }
                    }
                }
                Console.WriteLine(": Sum : " + total);
        }
    }
}
Output
Diagonals are : 22 63 64 69 83 : Sum : 301 _

More Example

Write A Program To Print One Dimensional Array In Reverse Order
Write A Program To Sort One Dimensional Array In Descending Order Using Non Static Method.
Write A Program To Sort One Dimensional Array In Desending Order Static Class Array Method.
Write A Program To Sort One Dimensional Array In Ascending Order Using Non Static Method.
Write A Program To Sort One Dimensional Array In Ascending Order Using Static Method.
Write A Program To Add The Diagonal Of Two-Dimensional Array.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.