Computer Scientists are Pretty Pessimistic

Friday 4 September 2015

WAP to find out the sum of non diagonal elements.

#include <stdio.h>
int main ()
{
    int a[50][50],i,j,row,col,sum=0;
    printf("Enter number of row:\n");
    scanf("%d",&row);
    printf("Enter number of column:\n");
    scanf("%d",&col);
    printf("Enter values:\n");
    for(i=0;i<row;i++) //input
    {
        for(j=0;j<col;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Main Matrix\n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<row;i++) //adding
    {
        if(i==0||i==(row-1))
        {
            for(j=1;j<=(col-2);j++)
                sum=sum+a[i][j];
        }
        else
            for(j=0;j<=(col-1);j=j+(col-1))
            {
                sum=sum+a[i][j];
            }
    }

    printf("Sum of non-diagonal elements is: %d",sum);
    return 0;
}

No comments:

Post a Comment