+ 1

To find the sum of both diagonal elements

write a program in c++

31st Mar 2017, 9:11 AM
vishal kalbande
vishal kalbande - avatar
3 Réponses
+ 9
#define N 3 int arr[N][N]; int sum1=0,sum2=0; for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { if(i==j) sum1+=arr[i][j]; if((i+j)==N-1) sum2+=arr[i][j]; } } cout<<sum1; cout<<sum2;
31st Mar 2017, 9:24 AM
Karl T.
Karl T. - avatar
+ 9
Sorry for all the typos I'm busy doing something else. lol But basically you add both diagonals that way.
31st Mar 2017, 9:31 AM
Karl T.
Karl T. - avatar
+ 5
use arrays to hold data,and then andd them
31st Mar 2017, 9:13 AM
Meharban Singh
Meharban Singh - avatar