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
#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:
- We declare an integer array
numbers[]with 5 elements. - The
sizeof the array is calculated usingsizeof(numbers) / sizeof(numbers[0]). - A variable
sumis initialized to0to store the accumulated sum. - The
forloop iterates from index0tosize - 1, adding each element tosum. - 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
#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:
- An integer array
numbers[]is declared with 5 elements. - The array size is determined using
sizeof(). - We initialize
sumto0andito0before starting the loop. - The
whileloop continues as long asiis less thansize. - Inside the loop, each array element is added to
sum, andiis incremented. - 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
#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:
- The integer array
numbers[]is initialized with 5 elements. - The size of the array is determined using
sizeof(). - We initialize
sumto0andito0. - The
doblock executes first, adding the current element tosum, then incrementingi. - The
whileconditioni < sizeis checked after execution. - 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:
forloop: Best when the number of iterations is known.whileloop: Useful when the number of elements is dynamic.do-whileloop: Ensures at least one execution before checking the condition.
