Sunday 9 September 2012

Array035

Input number in a matrix and print sum all the Left diagonal elements of the Matrix.

#include <stdio.h>
#include <conio.h>

void main ( )
{
    int        n1[3][3], i, j, s = 0;
    clrscr ( );

    printf ("\n\t\t\t Enter numbers in a matrix\n");
    printf("\n\t\t\t____________________________\n");
    for ( i = 0; i < 3; i++ )
    {           
                for ( j = 0; j < 3; j++)
                {            
                            printf("\n\t\t\t Position [%d] [%d]..:",i,j); scanf ("%d", &n1[i][j] );
                }
    }
    clrscr ( );

    printf("\n\n\n\n\t\t\t\t    Matrix\n\t\t\t_______________________________\n\n\t\t\t\t");
    for ( i = 0; i < 3; i++ )
    {                    
                for ( j = 0; j < 3; j++ )
                {                 
                            printf ("%3d ", n1[i][ j] );
                }
                printf ("\n\t\t\t\t");
    }

    printf("\n\n\t\t\t Left diagonal elements of the Matrix");
    printf("\n\n\t\t\t______________________________________");
    printf("\n\n\t\t\t\t");
    for ( i = 0; i < 3; i++ )
    {          
                for ( j = 0; j < 3; j++ )
                {          
                            if ( i == j )
                            {             
                                     printf ("\n\n\t\t\t Element of position [ %d ][ %d ] = %d",i,j,n1[i][j]);
                                     s = s + n1[i][ j];
                            }
                }

    }
    printf("\n\n\n\n\t\t\t Sum of the left diagonal elements = %d",s);
    getch ( );
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...