Tuesday, 12 August 2014

\\PROGRAM TO FIND THE SUM OF DIAGONAL ELEMENTS IN A 2-D ARRAY
\\CODE
#include<stdio.h>
#include<conio.h>
clrscr();
int main()

{
   
    int a[10][10],b[10][10];
    int i,j,row,col,sum=0;
    printf("\n Enter the rows and column of the matrix:");
    scanf("%d %d",&row,&col);
    printf("\n Enter the matrix a:");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&a[i][j]);
        }
        printf("\n");
    }
    printf("\n The matrix is as follows:\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf(" \t %d",a[i][j]);
        }
    printf("\n");
    }
    sum=0;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            if(i==j)
            {
                sum=sum+a[i][j];
            }
        }
    }
    printf("\n The resultant is: %d",sum);
    getch();
    return 0;
}

\\OUTPUT
Enter the rows and column of the matrix:2
2
 Enter the matrix a:4
8

9
6
The matrix is as follows:
         4       8
         9        6
The resultant is: 10

No comments:

Post a Comment

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