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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> void main() { int i, size; printf ( "C Program to print all even number in array \n" ); printf ( "First enter number of elements you want in array\n" ); scanf ( "%d" , &size); int arr[size]; for (i = 0; i < size; i++) { printf ( "Please give value for index %d : " ,i); scanf ( "%d" ,&arr[i]); } printf ( "Even number in array are\n" ); for (i = 0; i < size; i++){ if (arr[i]%2==0){ printf ( "%d \t" ,arr[i]); } } return 0; } |
Output
