Calculate Upper Triangle,Lower Triangle and Diagonals of a matrix

 #include <stdio.h>

int main (){

int r,c,n,ut,lt,Diagona;

scanf("%d",&n);

int a[20][20];

for(r=0;r<n;r++){

for(c = 0;c<n;c++){

printf("a[%d][%d] : \t",r,c);

scanf("%d",&a[r][c]);

}

}

for(r=0;r<n;r++){

for(c = 0;c<n;c++){

printf("%d ",a[r][c]);

}

printf("\n");

}

ut = lt = Diagona = 0;

for(r=0;r<n;r++){

for(c=0;c<n;c++){

if(r<c){

ut = ut + a[r][c];

}

}

}

for(r=0;r<n;r++){

for(c=0;c<n;c++){

if(r>c){

lt += a[r][c];

}

}

}

for(r=0;r<n;r++){

for(c=0;c<n;c++){

if(r == c){

Diagona += a[r][c];

}

}

}

printf("Upper Triangle = %d\nLower Triangle = %d\nDiagonals = %d",ut,lt,Diagona);

return 0;

}

Comments