C Program to print only odd numbers from an array
Learn how to print only the odd numbers stored in a C array using the modulo operator, with validated code, examples, a dry run, and complexity analysis.
An integer is odd when dividing it by 2 leaves a nonzero remainder. To print only the odd numbers from an array, visit each element and print it when the following condition is true:
number % 2 != 0
For example, the odd elements in 6, -5, 0, 13, 18, -21 are -5, 13, and -21.
C Program to Print Only Odd Numbers from an Array
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int foundOdd = 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;
}
}
printf("Odd elements: ");
for (int index = 0; index < size; index++) {
if (array[index] % 2 != 0) {
printf("%d ", array[index]);
foundOdd = 1;
}
}
if (!foundOdd) {
printf("None");
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 6
Enter 6 elements:
6 -5 0 13 18 -21
Odd elements: -5 13 -21
The values may be entered on one line or on separate lines because scanf() treats whitespace as a separator when reading integers.
How the Program Works
- The program validates the array size before reading any elements.
- The first loop stores the integers entered by the user.
- The second loop visits each stored element in its original order.
array[index] % 2calculates the remainder after division by2.- A nonzero remainder means the current element is odd, so the program prints it and sets
foundOddto1. - If no odd number is found, the program prints
None.
Here is a dry run for the sample array:
| Element | element % 2 | Is it printed? | Output so far |
|---|---|---|---|
| 6 | 0 | No | — |
| -5 | -1 | Yes | -5 |
| 0 | 0 | No | -5 |
| 13 | 1 | Yes | -5 13 |
| 18 | 0 | No | -5 13 |
| -21 | -1 | Yes | -5 13 -21 |
Only matching elements are printed; the program does not alter the values or their positions in the original array.
Why Use != 0 Instead of == 1?
For positive odd numbers, the remainder after division by 2 is usually 1. However, in C, a negative odd number can produce a remainder of -1:
13 % 2 = 1
-5 % 2 = -1
Both values are odd because both remainders are nonzero. Therefore, number % 2 != 0 works correctly for positive and negative odd numbers, while number % 2 == 1 can miss negative values.
Is Zero an Odd Number?
No. Zero is evenly divisible by 2:
0 % 2 = 0
The condition is false for zero, so the program correctly skips it.
What If the Array Has No Odd Numbers?
The foundOdd flag makes the no-match case clear:
Enter the number of elements: 4
Enter 4 elements:
2 8 0 -6
Odd elements: None
Without the flag, the output would contain only the label and would not explicitly tell the user that no odd elements were present.
Printing Versus Counting Odd Elements
Printing displays every odd value, while counting keeps a total of how many odd values were found. Both operations use array[index] % 2 != 0, but the action inside the condition differs:
- Use
printf()to display each matching value. - Increase a counter to calculate the number of matching values.
Time and Space Complexity
- Time complexity:
O(n), because each of thenarray elements is checked once. Reading the elements is alsoO(n), so the complete program remainsO(n). - Extra space complexity:
O(1)for filtering and printing because only a flag and a loop index are used. The input array itself requiresO(n)storage.
Common Mistakes
- Using
/instead of the modulo operator%. - Checking
number % 2 == 1, which can fail for negative odd numbers. - Using
== 0, which selects even numbers instead of odd numbers. - Treating zero as odd even though its remainder is zero.
- Forgetting to handle an array that contains no odd elements.
- Modifying the array when the task only requires printing selected values.
By checking for a nonzero remainder during one traversal, the program prints every odd element efficiently and preserves the array's original order.