google.com, pub-6167773875660516, DIRECT, f08c47fec0942fa0 Write a program to find the sum of the diagonal of a square matrix - 2nd puc computer science

Write a program to find the sum of the diagonal of a square matrix

Write a program to find the sum of the diagonal of a square matrix

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10][10],i,r,c,j,sum=0;

clrscr();

printf("Enter the order of matrix: \n");

scanf("%d%d",&r,&c);

if(r==c)

{

printf("Enter the elements\n");

for(i=0;i<r;i++)

         for(j=0;j<c;j++)

                  scanf("%d",&a[i][j]);

printf("Sum of the diagonal is\n");

for(i=0;i<r;i++)

{

                  for(j=0;j<c;j++)

                           if(i==j)

                           sum=sum+a[i][j];

}

printf("%d",sum);

}

else

printf("Matrix is not square, Diagonal sum is not possible");

getch();

}

Output:

 

run 1:

Enter the order of matrix:

3

3

Enter the elements

1 2 3

4 5 6

7 8 9

Sum of the diagonal is 15

 

run 2:

Enter the order of matrix:

2                                                                              

3                                                                              

Matrix is not square, Diagonal sum is not possible
Write a program to find the sum of the diagonal of a square matrix Write a program to find the sum of the diagonal of a square matrix Reviewed by Vision Academy on December 22, 2021 Rating: 5

No comments:

CheckOut

Powered by Blogger.