Monday, June 18, 2012

To find the sum of diagonal elements of the given matrix


/* Program to find the sum of diagonal elements of the given matrix */

#include<stdio.h>

#include<conio.h>

void main()

{

  int row,col,i,j,mat[100][100];

  int sum=0;

  clrscr();

  printf("Enter the number of rows:");

  scanf("%d",&row);

  printf("Enter the number of columns:");

  scanf("%d",&col);

  printf("\nEnter the elements:\n");

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

  {

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

    {

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

    }

  }

  printf("\n\nGIVEN MATRIX:\n");

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

  {

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

    {

      printf("\t%d",mat[i][j]);

    }

    printf("\n");

  }

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

  {

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

    {

      if(i==j)

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

    }

  }

  printf("\nSum of diagonal elements of the given matrix = %d",sum);

  getch();

}

 

OUTPUT

Enter the number of rows:3

Enter the number of columns:3

 

Enter the elements:

2

4

6

8

1

3

5

7

9

 

 

GIVEN MATRIX:

        2       4       6

        8       1       3

        5       7       9

 

Sum of diagonal elements of the given matrix = 12

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.