C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Sum of array elements

C Program: Sum of array elements

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i, sum = 0;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

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

        scanf("%d", &arr[i]);

    }

 

    // Calculate sum of elements

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

        sum += arr[i];

    }

 

    // Display result

    printf("\nSum of array elements = %d\n", sum);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50

OUTPUT :
Sum of array elements = 150

Explanation

  1. The program first takes input for the number of array elements (n).
  2. It reads all array values using a for
  3. Another for loop adds up all the elements and stores the result in sum.
  4. Finally, it prints the total sum.
  5. Arrays allow storing multiple numbers, and loops make it easy to perform operations on them collectively.
  6. Here, the loop iterates through all elements and keeps a running total using the accumulator variable sum.

 

C Program: Sum of array elements

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, sum = 0;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    while(i < n) {

        scanf("%d", &arr[i]);

        i++;

    }

 

    // Calculate sum of array elements

    i = 0;

    while(i < n) {

        sum += arr[i];

        i++;

    }

 

    // Display result

    printf("\nSum of array elements = %d\n", sum);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

OUTPUT :
Sum of array elements = 50

Explanation

  1. The program first takes the total number of elements (n).
  2. A while loop is used to input all array elements one by one.
  3. Another while loop iterates through the array to calculate the total sum.
  4. The sum is then displayed as output.
  5. The while loop continues to execute as long as the condition (i < n) is true.
  6. It is particularly useful when you want manual control over loop variables.

 

C Program: Sum of array elements

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, sum = 0;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using do...while loop

    printf("Enter %d elements:\n", n);

    do {

        scanf("%d", &arr[i]);

        i++;

    } while(i < n);

 

    // Calculate sum of array elements using do...while loop

    i = 0;

    do {

        sum += arr[i];

        i++;

    } while(i < n);

 

    // Display result

    printf("\nSum of array elements = %d\n", sum);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
2 4 6 8 10

OUTPUT :
Sum of array elements = 30

Explanation

  1. The program begins by asking for the number of elements (n) in the array.
  2. Using a do..while loop, it takes input for all elements — this ensures the loop runs at least once.
  3. Another do..while loop calculates the total sum of all elements.
  4. The result is displayed at the end.
  5. The do..while loop executes the loop body before checking the condition.
  6. This structure guarantees that the user gets to input at least one value, making it suitable for input-based programs.