📜  上下三角形元素之和 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:36.904000             🧑  作者: Mango

代码示例1
#include 

int main()
{

    int a[3][3],i,j,upper_sum=0,lower_sum=0;
    printf("Enter elements for the matrix : \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            printf("Matrix[%d][%d]: \n",i,j);
            scanf("%d",&a[i][j]);
        }
    }

    printf("The Matrix: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }

    printf("Getting sum... \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            if(i < j){
                printf("The Upper Triangle elements are %d\n",a[i][j]);
                upper_sum+=a[i][j];
            }
            if(i > j){
                printf("The lower Triangle elements are %d\n",a[i][j]);
                lower_sum+=a[i][j];
            }
            
            
        }
       
    }
    
    printf("Sum of Uppper triangles are : %d\n",upper_sum);
    printf("Sum of lower triangles are : %d\n",lower_sum);



    return 0;
}