Program to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix

Posted in

Program to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    Here in this article, we will write a program to find the sum of non-diagonal elements and separate them from elements above and below the diagonal of a matrix. We will consider only one diagonal to keep things simple: the left-to-right one.

    The Sum of Elements Above and Below the Main Diagonal of a Matrix

    For example, if we have the following matrix:

    3  4  5

    6  7  9

    5  4  6

    The sum of the above diagonal elements of the matrix will be 4+5+9 =18, and the sum of the below diagonal elements of the same will be 6+5+4 = 15.

    To implement a program that can do so, we need to use a nested for loop, i.e., a for loop inside a for loop. Hence, you can implement the program in any programming language with this logic.

    We will start by first asking the user to enter elements of an array. Once we get these, we will use the nested for loop to calculate the sum of the elements below and above the diagonal. Finally, we display the same.

    C Program to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix

    #include<stdio.h>
    int main()
    {
        int arr[4][4], ad = 0, bd = 0;
        printf("Enter the elements in the array: \n");
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                scanf("%d", &arr[i][j]);
                if(j > i)
                    ad += arr[i][j];
                else if(i > j)
                    bd += arr[i][j];
            }
        }
        printf("The matrix is:\n");
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                printf("%d\t", arr[i][j]);
            }
            printf("\n");
        }
        printf("The sum of above diagonal elements is: %d \n", ad);
        printf("The sum of below diagonal elements is: %d \n", bd);
        return 0;
    }

    Output:

    Enter the elements in the array: 
    3 4 5 1 2 3 0 8 9 7 6 4
    The matrix is:
    3       4       5
    1       2       3
    0       8       9
    The sum of above diagonal elements is: 12 
    The sum of below diagonal elements is: 9 

    C++ Program to Find The Sum of Elements Above and Below the Main Diagonal of a Matrix

    #include<iostream>
    using namespace std;
    int main()
    {
        int arr[4][4], ad = 0, bd = 0;
        cout << "Enter the elements in the array:" << endl;
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                cin >> arr[i][j];
                if(j > i)
                    ad += arr[i][j];
                else if(i > j)
                    bd += arr[i][j];
            }
        }
        cout << "The matrix is:" << endl;
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                cout << arr[i][j] << " ";
            }
            cout << endl;
        }
        cout << "The sum of above diagonal elements is: " << ad << endl;
        cout << "The sum of below diagonal elements is: " << bd;
        return 0;
    }

    Output:

    Enter the elements in the array:
    3 4 5 1 2 7 8 6 4 3 2 0
    The matrix is:
    3 4 5 
    1 2 7 
    8 6 4 
    The sum of above diagonal elements is: 16
    The sum of below diagonal elements is: 15

    Python Program to Find The Sum of Elements Above and Below the Main Diagonal of a Matrix

    arr=[[0,0,0],[0,0,0],[0,0,0]]
    ad=0
    bd=0
    print("Enter the elements in the array:")
    for i in range(3):
        for j in range(3):
            arr[i][j] = int(input())
         if j>i:
             ad+=arr[i][j]
         else:
             if i>j:
                 bd+=arr[i][j]
    print("The sum of above diagonal elements is:",ad)
    print("The sum of below diagonal elements is:",bd)

    Output:

    Enter the elements in the array:
    
    3
    
    4
    
    5
    
    6
    
    7
    
    9
    
    5
    
    4
    
    6
    
    The sum of above diagonal elements is: 18
    
    The sum of below diagonal elements is: 15

    Java Program to Find The Sum of Elements Above and Below the Main Diagonal of a Matrix

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            int arr[][] = new int[4][4];
            int ad = 0, bd = 0;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the elements in the array:");
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    arr[i][j] = sc.nextInt();
                    if (j > i)
                        ad += arr[i][j];
                    else if (i > j)
                        bd += arr[i][j];
                }
            }
            System.out.println("The matrix is:");
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(arr[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println("The sum of above diagonal elements is: " + ad);
            System.out.println("The sum of below diagonal elements is: " + bd);
        }
    }

    Output:

    Enter the elements in the array:
    3 2 4 5 6 7 8 1 2 3 0 9
    The matrix is:
    3 2 4 
    5 6 7 
    8 1 2 
    The sum of above diagonal elements is: 13
    The sum of below diagonal elements is: 14

    Conclusion

    With the abovementioned program, you can find the sum of elements above and below the diagonal of a matrix in C++ and Python programming languages. In the above code, we have predefined the size of the matrix.

    You can, however, come up with a program that asks the user to enter the size of the matrix too. Also, the code above only considers the left-to-right diagonal. However, you can consider the other one or both.

    People are also reading:

    Leave a Comment on this Post

    0 Comments