Monday, 24 February 2014

Write a ‘C’ program to find out sum of principal diagonal elements of a square matrix.

#include<stdio.h>
main()
{   
    int a[100][100],size,i,j,sum=0;
    printf("\nEnter size of the matrix=");
    scanf("%d",&size);
    printf("\nEnter the matrix elements=");
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("\nThe matrix is\n");
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            printf("%6d",a[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<size;i++)
    {
        sum=sum+a[i][i];
    }
    printf("\nThe sum of principal diagonal elements is %d",sum);
}

No comments:

Post a Comment