Matrix - Sum of Minor Diagonal

 Coding Problem Keys 

 Matrix - Sum of Minor Diagonal 

 Problem Statement 

Maria, class teacher of standard twelve has set out on a vacation to Coorg. They have booked accommodation in a resort and the resort authorities organize Campfires every night as a part of their daily activities. But she can't take all the students of her class on vacation. So she planned to keep a test, those who score full marks on the test are eligible to go on a vacation. The test consists of a single question based on a 2D array, Given a square matrix x[5][5], write a code to compute the sum of minor diagonal. Help her to solve this.

 Input Format 

The input consists of a 5*5 square matrix.

 Output Format 

The output prints the sum of minor diagonal.

 Example Input/Output 1 

 Input 

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

 Output 

65

 Explanation 

The sum of minor diagonal elements, 5 + 9 + 13 + 17 + 21 is 65.
Hence 65 is printed as the output.

 Example Input/Output 2 

 Input 

2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6 12 18 24 30

 Output 

50

 Note 

The program will not be evaluated if "Submit Code" is not done atleast once.
Extra spaces and new line characters in the program output will also result in the testcase failing.

 Solution 

 Programming Language: C Language 

#include <stdio.h>
int main(){
    int arr[5][5];
    int i,j,sum=0;
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
            scanf("%d",&arr[i][j]);
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
            if(i+j==4)
                sum+=arr[i][j];
    printf("%d",sum);
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Toggle Consonants Adjacent to Vowels

String Pattern - Inverted U

Top Scoring Batsman - TEST ODI 20-20

Desktop Products

Mango Distribution

Java - Method Overriding - Area

Microsoft