In this algorithm , we have to find the sum of right diagonal elements of matrix . Supoose a 2d matrix , then it has 2 diagonals i.e left diagonal & right diagonal . So, in right diagonal ( / in this way) , ( i+j) is equal to (m-l) So,
when the condition of this diagonal is true, the element is added in variable sum . & hence the sum of right diagonal is found .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sum_right=0;
clrscr();
printf("\n program to find sum of right 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)==(m-1))
{ sum_right = sum_right + 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 right diagonal elements = %d",sum_right);
getch();
}
when the condition of this diagonal is true, the element is added in variable sum . & hence the sum of right diagonal is found .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sum_right=0;
clrscr();
printf("\n program to find sum of right 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)==(m-1))
{ sum_right = sum_right + 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 right diagonal elements = %d",sum_right);
getch();
}

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