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
size
of the array is calculated usingsizeof(numbers) / sizeof(numbers[0])
. - A variable
sum
is initialized to0
to store the accumulated sum. - The
for
loop iterates from index0
tosize - 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
sum
to0
andi
to0
before starting the loop. - The
while
loop continues as long asi
is less thansize
. - Inside the loop, each array element is added to
sum
, andi
is 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
sum
to0
andi
to0
. - The
do
block executes first, adding the current element tosum
, then incrementingi
. - The
while
conditioni < size
is 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:
for
loop: Best when the number of iterations is known.while
loop: Useful when the number of elements is dynamic.do-while
loop: Ensures at least one execution before checking the condition.