Fundamentals of Computer Programming - Old Questions

5. Write a program to check whether the diagonal elements of a (4*4) matrix are all Zero.

6 marks | Asked in 2073

#include<stdio.h>

void main()

{

    int mat[4][4];

    int i,j,flag;

    printf("Enter the elements of the matrix\\n");

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

    {

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

        {

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

        }

    }

    printf("The matrix\\n");

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

    {

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

        {

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

        }

        printf("\\n");

    }

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

    {

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

        {

            if(i==j)

            {

                if(mat[i][j]==0)

                    flag=0;

                else

                    flag=1;

            }

        }

    }

    if(flag==1)

        printf("The diagonal elements of matrix are not zero");

    else

        printf("The diagonal elements of matrix are all zero");

}