Sum of Numbers in an Array Using Loops in C

To calculate the sum of numbers in an array using loops in C, we iterate through each element of the array and add it to a sum variable. This can be achieved using for, while, or do-while loops.

In this tutorial, we will explore different ways to compute the sum of an array with step-by-step explanations.


Examples of Summing an Array Using Loops

1. Using a for Loop to Calculate Sum

In this example, we will declare an array of integers and use a for loop to iterate through each element, adding them to a sum variable.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0;

    // Loop through array and add each element to sum
    for (int i = 0; i < size; i++) {
        sum += numbers[i];
    }

    printf("Sum of array elements: %d\n", sum);
    return 0;
}

Explanation:

  1. We declare an integer array numbers[] with 5 elements.
  2. The size of the array is calculated using sizeof(numbers) / sizeof(numbers[0]).
  3. A variable sum is initialized to 0 to store the accumulated sum.
  4. The for loop iterates from index 0 to size - 1, adding each element to sum.
  5. After the loop completes, the total sum is printed using printf().

Output:

Sum of array elements: 15

2. Using a while Loop to Calculate Sum

In this example, we will use a while loop instead of a for loop to iterate over the array elements and compute the sum.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {3, 6, 9, 12, 15};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0, i = 0;

    // Using while loop to calculate sum
    while (i < size) {
        sum += numbers[i];
        i++;
    }

    printf("Sum of array elements: %d\n", sum);
    return 0;
}

Explanation:

  1. An integer array numbers[] is declared with 5 elements.
  2. The array size is determined using sizeof().
  3. We initialize sum to 0 and i to 0 before starting the loop.
  4. The while loop continues as long as i is less than size.
  5. Inside the loop, each array element is added to sum, and i is incremented.
  6. Finally, the total sum is displayed using printf().

Output:

Sum of array elements: 45

3. Using a do-while Loop to Calculate Sum

In this example, we use a do-while loop, which ensures at least one execution of the loop body before checking the condition.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int numbers[] = {5, 10, 15, 20, 25};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    int sum = 0, i = 0;

    // Using do-while loop to calculate sum
    do {
        sum += numbers[i];
        i++;
    } while (i < size);

    printf("Sum of array elements: %d\n", sum);
    return 0;
}

Explanation:

  1. The integer array numbers[] is initialized with 5 elements.
  2. The size of the array is determined using sizeof().
  3. We initialize sum to 0 and i to 0.
  4. The do block executes first, adding the current element to sum, then incrementing i.
  5. The while condition i < size is checked after execution.
  6. Finally, the total sum is printed.

Output:

Sum of array elements: 75

Conclusion

We explored different ways to calculate the sum of an array using loops in C:

  1. for loop: Best when the number of iterations is known.
  2. while loop: Useful when the number of elements is dynamic.
  3. do-while loop: Ensures at least one execution before checking the condition.