In this algorithm , we have to find the sum of left diagonal elements of matrix . Supoose a 2d matrix , then it has 2 diagonals i.e left diagonal & right diagonal . So, in left diagonal ( \ in this way) , i==j like a[1][1] , a[2][2]. So,
when the condition of this diagonal is true, the element is added in variable sum . & hence the sum of left diagonal is found .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sumleft=0;
clrscr();
printf("\n program to find sum of left diagonal elements of matrix");
printf("\n (rows & columns should be same)");
printf("\n\n enter no. of rows & cloumns : ");
scanf("%d",&m);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
if(i==j)
{ sumleft = sumleft + a[i][j];
}
}
}
printf("\n displaying matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
}
printf("\n");
}
printf("sum of left diagonal elements = %d",sumleft);
getch();
}
when the condition of this diagonal is true, the element is added in variable sum . & hence the sum of left diagonal is found .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sumleft=0;
clrscr();
printf("\n program to find sum of left diagonal elements of matrix");
printf("\n (rows & columns should be same)");
printf("\n\n enter no. of rows & cloumns : ");
scanf("%d",&m);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
if(i==j)
{ sumleft = sumleft + a[i][j];
}
}
}
printf("\n displaying matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
}
printf("\n");
}
printf("sum of left diagonal elements = %d",sumleft);
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.