## C Program To Find Sum of Major and Minor Diagonal Elements of Matrix

Here, we have listed How To Calculate Sum of Major and Minor Diagonal Elements of a Matrix in C Programming Language. It is important that we should know** How A For Loop Works** before getting further with the C Program Code.

#### What are Major Diagonal Elements of a Matrix?

The Major Diagonal Elements are the ones that occur from Top Left of Matrix Down To Bottom Right Corner. The Major Diagonal is also known as **Main Diagonal**.

#### What are Minor Diagonal Elements of a Matrix?

The Minor Diagonal Elements are the ones that occur from Top Right of Matrix Down To Bottom Left Corner.

**Also Read: C Program To Calculate Sum of Array Elements**

#### Method 1: Find Sum of Major Diagonal Elements in C Programming

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<stdio.h> int main() { int arr[3][3]; int i, j, sum = 0; printf("\nEnter 9 Elements for 3*3 Matrix:\n"); for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } printf("\nMatrix\n"); for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { printf("%d\t", arr[i][j]); } printf("\n"); } for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { if(i == j) sum = sum + arr[i][j]; } } printf("\nSum of Major Diagonal Elements: \t%d", sum); printf("\n"); return 0; } |

#### Output

#### C Program To Find Sum of Minor Diagonal Elements

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<stdio.h> int main() { int arr[3][3]; int i, j, sum = 0; printf("\nEnter 9 Elements for 3*3 Matrix:\n"); for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { scanf("%d", &arr[i][j]); } } printf("\nMatrix\n"); for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { printf("%d\t",arr[i][j]); } printf("\n"); } for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { if(i + j == 2) sum = sum + arr[i][j]; } } printf("\nSum of Minor Diagonal Elements: \t%d",sum); printf("\n"); return 0; } |

#### Output

In case you get any Compilation Errors with this C Program Code To Find Major and Minor Diagonals of Matrix or if you have any doubt about it, mention it in the Comment Section.

Excellent Program! Thanks! Every other program that I came across was very confusing! You have specified correctly the difference between Major and Minor Diagonals.

You’re welcome Derek! Stay in touch!

Phew!! Finally I got a correct working c program code on finding sum of major diagonals and minor diagonals and that too with explanation.

I was getting confused on which is Major Diagonal and which is Minor Diagonal. Thanks for the briefing above.

minor diagonal formula is wrong it will not work for -ve no’s .

Major diagonal = from top-left to bottom-right

Minor diagonal = from top-right to bottom-left

Actually the values you used for filling your matrix are very badly chosen because you made a magic square, which is to say a 3×3 matrix whose diagonals and middle line and column sum up to the same value (15) so it does not prove it works at all.