Thursday, 21 June 2012

write a c program to calculate sum of element of upper triangle in n*n matrix


/*write a c program to calculate sum of element of upper triangle
in n*n matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf("\nenter the element of matrix :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nthe display of matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<j)
sum=sum+a[i][j];
}
}
printf("sum is :%d",sum);
getch();
}

/*
enter the element of matrix :1 4 5                                            
2 3 6                                                                        
7 8 9                                                                        
                                                                             
the display of matrix is:                                                    
        1       4       5                                                    
        2       3       6                                                    
        7       8       9                                                    
sum is :15                                                                    
*/

No comments: