Friday, December 4, 2020

Problem solving through Programming In C Session-1 Problem - 3 Programming Solution

 

Session-1 Problem - 3

Due on 2020-12-04, 11:00 IST
Write a program to find the total sum of the diagonal elements of a square matrix. 
Your last recorded submission was on 2020-12-04, 10:25 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
3
int main()
4
{
5
    int a[10][10], n; 
6
    scanf("%d", &n); //Size of the matrix is taken from test data
7
8
//Use the printf statement as:
9
//printf("Sum of the diagonal elements is = %d",variable_name);
10
int i,j,sum1=0, sum2=0, sum=0;
11
12
for(i=0; i<n; i++)
13
{
14
    for(j=0; j<n; j++)
15
    {
16
        scanf("%d", &a[i][j]);
17
    }
18
}
19
20
21
 for(i=0;i<n;i++)
22
    {
23
         sum1=sum1 + a[i][i];
24
    }
25
      
26
    for(i=0;i<n;i++)
27
    {
28
         sum2=sum2 + a[i][n-1-i];
29
    }
30
  sum = sum1+sum2;
31
 
32
    printf("Sum of the diagonal elements is = %d",sum);
33
}

No comments:

Post a Comment