In this tutorial, we will learn a writing program in c to print all odd numbers present in an array. As we know that odd number is a number that is not divisible by 2.
Our logic will be like, iterate each array element and check if it is divisible by 2 or not. If it is not divisible by 2 then we will print the element as an odd number. If divisible by 2 then we will skip the printing.
C Program to print all odd numbers in array
#include <stdio.h>
int main() {
int i, arraySize;
printf("=== C Program to print all odd numbers in array ===\n\n");
printf("Enter the number of elements you want in the array: ");
scanf("%d", &arraySize);
int arr[arraySize];
// Taking input for the array
for(i = 0; i < arraySize; i++) {
printf("Enter value for index %d: ", i);
scanf("%d", &arr[i]);
}
// Printing odd numbers in the array
printf("Odd numbers in the array are:\n");
for(i = 0; i < arraySize; i++) {
if(arr[i] % 2 != 0) {
printf("%d ", arr[i]);
}
}
return 0;
}
Output
=== C Program to print all odd numbers in array ===
Enter the number of elements you want in the array: 6
Enter value for index 0: 7
Enter value for index 1: 3
Enter value for index 2: 4
Enter value for index 3: 5
Enter value for index 4: 9
Enter value for index 5: 8
Odd numbers in the array are:
7 3 5 9
What did you think?