Sum of diagonals of Matrix.

Program to sum up the diagonal of a matrix


SOURCE CODE


import java.io.*;
import java.util.*;
public class Diagonal_Sum
{
    public static void main(String args[])
    {
        int rd=0,ld=0;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of array     :");
        int num=sc.nextInt();
        int ar[][]=new int[num][num];
        for(int i=0;i<num;i++)
        {
            for(int j=0;j<num;j++)
            {
                System.out.print("Input a number    :");
                ar[i][j]=sc.nextInt();
            }
        }
        for(int i=0;i<num;i++)
        {
            for(int j=0;j<num;j++)
            {
                System.out.print(ar[i][j]+"\t");
            }
            System.out.println();
        }
        for(int i=0;i<num;i++)
        {
            ld=ld+ar[i][i];
        }
        int k=num-1;
        for(int i=0;i<num;i++)
        {
            rd=rd+ar[i][k];
            k--;
        }
        System.out.println("The sum of right diagonal = "+rd);
        System.out.println("The sum of left diagonal = "+ld);
    }
}


OUTPUT




If you have any question then leave a comment below I will do my best to answer that question.



Comments

Popular posts from this blog

Pattern of your own name.

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Designing Patterns - Print 'V'.