C Program to find the sum of all odd numbers in an array
Learn how to find the sum of all odd numbers stored in a C array using the modulo operator, with validated code, examples, and a dry run.
To find the sum of all odd numbers in an array, examine each element and add it to a running total when dividing it by 2 produces a nonzero remainder.
The condition for an odd integer in C is:
number % 2 != 0
For example, the odd elements in 6, -5, 0, 13, 18, 21 are -5, 13, and 21. Their sum is:
-5 + 13 + 21 = 29
C Program to Find the Sum of Odd Array Elements
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int foundOdd = 0;
long long oddSum = 0;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
for (int index = 0; index < size; index++) {
if (array[index] % 2 != 0) {
oddSum += array[index];
foundOdd = 1;
}
}
if (!foundOdd) {
printf("No odd elements were found.\n");
}
printf("Sum of odd elements = %lld\n", oddSum);
return 0;
}
Sample Output
Enter the number of elements: 6
Enter 6 elements:
6 -5 0 13 18 21
Sum of odd elements = 29
The input values may be entered on one line or on separate lines because scanf() treats whitespace as a separator.
How the Program Works
oddSumis initialized to0before any values are processed.foundOddrecords whether at least one odd element occurs in the array.- The first loop reads and stores the array elements.
- The second loop checks each value using
array[index] % 2 != 0. - When the condition is true, the current value is added to
oddSum, andfoundOddbecomes1. - After the traversal, the program reports the final sum.
Here is a dry run for the sample array:
| Element | Odd? | Calculation | Running sum |
|---|---|---|---|
| 6 | No | Skip | 0 |
| -5 | Yes | 0 + (-5) | -5 |
| 0 | No | Skip | -5 |
| 13 | Yes | -5 + 13 | 8 |
| 18 | No | Skip | 8 |
| 21 | Yes | 8 + 21 | 29 |
The calculation does not modify the array; it only reads each element and updates a separate running total.
Why Use != 0 Instead of == 1?
Positive odd numbers normally produce a remainder of 1, but negative odd numbers can produce -1 in C:
13 % 2 = 1
-5 % 2 = -1
Both remainders are nonzero, so both numbers are odd. The condition % 2 != 0 correctly includes positive and negative odd values, whereas % 2 == 1 can incorrectly skip negative values.
Why Use long long for the Sum?
Although the individual elements have type int, their total can exceed the range of an int when several large values are added. A long long running total provides a wider range on common C implementations.
Because oddSum is a long long, the program prints it using the %lld format specifier.
What About Zero and Negative Numbers?
Zero is not odd because 0 % 2 is 0, so it is skipped. Negative odd values have a nonzero remainder and are included in the sum.
For example, the odd values in -9, 4, -3, 8 add up to:
-9 + (-3) = -12
What If There Are No Odd Elements?
When the array contains only even values, oddSum remains 0 and foundOdd remains false:
Enter the number of elements: 4
Enter 4 elements:
2 -8 0 14
No odd elements were found.
Sum of odd elements = 0
The flag distinguishes an array with no odd elements from an array whose odd elements cancel out to zero, such as -5, 5, 8.
Can the Sum Be Calculated While Reading Input?
Yes. The modulo condition and addition can be performed immediately after each successful input. This combines reading and processing into one loop.
The complete program uses separate input and calculation loops to keep each step easy to understand and to preserve the array for later operations. Both approaches take O(n) time.
Time and Space Complexity
- Time complexity:
O(n), because each of thenarray elements is tested once. Reading the input is also linear, so the complete program remainsO(n). - Extra space complexity:
O(1)for the summing operation because only a running total, a flag, and a loop index are needed. The input array itself usesO(n)space.
Common Mistakes
- Adding every element instead of only values for which
% 2 != 0. - Checking
% 2 == 1, which can miss negative odd numbers. - Using
/instead of%to test whether a number is odd. - Forgetting to initialize
oddSumto0. - Treating zero as an odd number.
- Printing a
long longtotal with%dinstead of%lld.
By testing for a nonzero remainder and maintaining a running total, the program finds the sum of all odd array elements in one traversal.