Program to Find Sum of Diagonal, and Non Diagonal Elements of 3 X 3 Matrix

Program to Find Sum of Diagonal, and Non Diagonal Elements of 3 X 3 Matrix

  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in C
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in C++
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in Python
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in PHP
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in Java
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in Java Script
  • Write a program to Find Sum of Diagonal, and Non Diagonal Elements of Matrix in C#

Explanation:

Steps:

  1. Input:
    • Read the 3×3 matrix.
  2. Compute Diagonal Sum:
    • Loop through the matrix, summing elements where i=j.
  3. Compute Non-Diagonal Sum:
    • Calculate the total sum of all elements in the matrix.
    • Subtract the diagonal sum from the total sum to get the non-diagonal sum.
  4. Output:
    • Display the diagonal sum and non-diagonal sum.

An effective method for locating a target element in a sorted array is binary search. It divides the search interval in half repeatedly.

Program to Find Sum of Diagonal, and Non Diagonal Elements of 3X3 Matrix

#include <stdio.h>

int main() {
    int matrix[3][3], diagonalSum = 0, nonDiagonalSum = 0;

    // Input elements for the matrix
    printf("Enter elements for the 3x3 matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("Enter element matrix[%d][%d]: ", i + 1, j + 1);
            scanf("%d", &matrix[i][j]);

            if (i == j || i + j == 2)
                diagonalSum += matrix[i][j];
            else
                nonDiagonalSum += matrix[i][j];
        }
    }

    // Display the results
    printf("\nSum of diagonal elements: %d\n", diagonalSum);
    printf("Sum of non-diagonal elements: %d\n", nonDiagonalSum);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int matrix[3][3], diagonalSum = 0, nonDiagonalSum = 0;

    // Input elements for the matrix
    cout << "Enter elements for the 3x3 matrix:" << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Enter element matrix[" << i + 1 << "][" << j + 1 << "]: ";
            cin >> matrix[i][j];

            if (i == j || i + j == 2)
                diagonalSum += matrix[i][j];
            else
                nonDiagonalSum += matrix[i][j];
        }
    }

    // Display the results
    cout << "\nSum of diagonal elements: " << diagonalSum << endl;
    cout << "Sum of non-diagonal elements: " << nonDiagonalSum << endl;

    return 0;
}

matrix = []
print("Enter elements for the 3x3 matrix:")
for i in range(3):
    row = []
    for j in range(3):
        value = int(input(f"Enter element matrix[{i+1}][{j+1}]: "))
        row.append(value)
    matrix.append(row)

# Calculate sums
diagonal_sum = 0
non_diagonal_sum = 0

for i in range(3):
    for j in range(3):
        if i == j or i + j == 2:
            diagonal_sum += matrix[i][j]
        else:
            non_diagonal_sum += matrix[i][j]

# Display the results
print(f"\nSum of diagonal elements: {diagonal_sum}")
print(f"Sum of non-diagonal elements: {non_diagonal_sum}")

<?php
$matrix = array();
$diagonalSum = 0;
$nonDiagonalSum = 0;

// Input elements for the matrix
echo "Enter elements for the 3x3 matrix:\n";
for ($i = 0; $i < 3; $i++) {
    for ($j = 0; $j < 3; $j++) {
        $matrix[$i][$j] = (int)readline("Enter element matrix[" . ($i + 1) . "][" . ($j + 1) . "]: ");
        if ($i == $j || $i + $j == 2) {
            $diagonalSum += $matrix[$i][$j];
        } else {
            $nonDiagonalSum += $matrix[$i][$j];
        }
    }
}

// Display the results
echo "\nSum of diagonal elements: $diagonalSum\n";
echo "Sum of non-diagonal elements: $nonDiagonalSum\n";
?>

import java.util.Scanner;

public class MatrixDiagonalSum {
    public static void main(String[] args) {
        int[][] matrix = new int[3][3];
        int diagonalSum = 0, nonDiagonalSum = 0;
        Scanner scanner = new Scanner(System.in);

        // Input elements for the matrix
        System.out.println("Enter elements for the 3x3 matrix:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print("Enter element matrix[" + (i + 1) + "][" + (j + 1) + "]: ");
                matrix[i][j] = scanner.nextInt();

                if (i == j || i + j == 2) {
                    diagonalSum += matrix[i][j];
                } else {
                    nonDiagonalSum += matrix[i][j];
                }
            }
        }

        // Display the results
        System.out.println("\nSum of diagonal elements: " + diagonalSum);
        System.out.println("Sum of non-diagonal elements: " + nonDiagonalSum);

        scanner.close();
    }
}

let matrix = [];
let diagonalSum = 0;
let nonDiagonalSum = 0;

console.log("Enter elements for the 3x3 matrix:");
for (let i = 0; i < 3; i++) {
    let row = [];
    for (let j = 0; j < 3; j++) {
        let value = parseInt(prompt(`Enter element matrix[${i+1}][${j+1}]:`));
        row.push(value);

        if (i === j || i + j === 2) {
            diagonalSum += value;
        } else {
            nonDiagonalSum += value;
        }
    }
    matrix.push(row);
}

// Display the results
console.log(`\nSum of diagonal elements: ${diagonalSum}`);
console.log(`Sum of non-diagonal elements: ${nonDiagonalSum}`);

using System;

class MatrixDiagonalSum
{
    static void Main()
    {
        int[,] matrix = new int[3, 3];
        int diagonalSum = 0, nonDiagonalSum = 0;

        // Input elements for the matrix
        Console.WriteLine("Enter elements for the 3x3 matrix:");
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write($"Enter element matrix[{i + 1}][{j + 1}]: ");
                matrix[i, j] = int.Parse(Console.ReadLine());

                if (i == j || i + j == 2)
                {
                    diagonalSum += matrix[i, j];
                }
                else
                {
                    nonDiagonalSum += matrix[i, j];
                }
            }
        }

        // Display the results
        Console.WriteLine($"\nSum of diagonal elements: {diagonalSum}");
        Console.WriteLine($"Sum of non-diagonal elements: {nonDiagonalSum}");
    }
}

List of All Programs