sum of diagonal elements of a matrix in c

#include <stdio.h>
void main ()
{
    int i,j,matrix[10][10],m,n,sum=0;

    printf("Enter number of rows of matrix  : ");
    scanf("%d", &m);
    printf("Enter number of columns of matrix  : ");
    scanf("%d", &n);

    printf("\n");

    if(m==n)
    {
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("Enter element of matrix [%d][%d]: ", i, j);
                scanf("%d", &matrix[i][j]);
            }
        }

        printf("\n");

        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                {
                    sum=sum+matrix[i][j];
                }
            }
        }

        printf("The sum of diagonal elements of the matrix = %d\n",sum);
    }

    else
        printf("The matrix does not have diagonal elements");
}



3.75
4
Awgiedawgie 440215 points

                                    #include&lt;stdio.h&gt;
&nbsp;
void main()
{
&nbsp;&nbsp;&nbsp;&nbsp;int mat[12][12];
&nbsp;&nbsp;&nbsp;&nbsp;int i,j,row,col,sum=0;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Enter the number of rows and columns for 1st matrix\n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d%d&quot;,&amp;row,&amp;col);
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Enter the elements of the matrix\n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i&lt;row;i++)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(j=0;j&lt;col;j++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(&quot;%d&quot;,&amp;mat[i][j]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;The sum of diagonal elements of a square matrix = %d\n&quot;,sum);
}

3.75 (4 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source