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