Calculate Sum of Array Elements C Program

By | November 16, 2015

C Program To Calculate Sum of Array Elements

Learn How To Calculate Sum of Array Elements in C Programming. Here, the Array is a 2-Dimensional Array or a Matrix. It is important that we should know How For Loop Works before getting further with the C Program Code.

Since this is a 2 Dimensional Array, we are taking 2 For Loops to add elements of Rows and Columns.

Also Read: C Program To Find Transpose of Matrix

Note: This C Program To Find Addition of Array Elements is developed using gEdit Editor and compiled using GCC in Linux Ubuntu Operating System.

C Program To Find Sum of Array Elements

#include<stdio.h>

int main()
{
    int arr[10][10];
    int row, col, sum = 0, i = 0, j = 0;
    printf("\nEnter Number of Rows:  ");
    scanf("%d", &row);
    printf("\nEnter Number of Columns:  ");
    scanf("%d", &col);
    printf("Enter Matrix Elements\n");
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    printf("\nMatrix Elements\n");
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("%d\t", arr[i][j]);
        }
            printf("\n");
    }
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            sum = sum + arr[i][j];
        }
    }
    printf("\nSum of Array Elements: %d", sum);
    printf("\n");
    return 0;
}

Output

How To Calculate Sum of Array Elements in C

If you have any compilation errors or doubts in this C Program To Add Array Elements, let us know about it in the Comment Section below.

Let's Discuss