Skip to content

Main diagonal sum

Java loop over diagonal

Flowchart (ISO 5807)

StartInput a, nsum = 0i = 0, n − 1, 1sum = sum + a[i][i]Return sumEndFigure 1 — diagonalSum

Source code

Java
static int diagonalSum(int[][] a, int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += a[i][i];
    }
    return sum;
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
loop over diagonal
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples