# C Program to Print Even and Odd Numbers in an Array

«
»

Odd Number:
A number is said to be an odd number if it is not completely divisible by 2.
In other words, if a number is divided by 2 and the remainder is 1, it is said to be an odd number.
Example: 23, 19

Even Number:
A number is said to be an even number if it is completely divisible by 2.
In other words, if a number is divided by 2 and leaves a remainder of 0, then it is said to be an even number.
Example: 34, 15

Problem Description

Write a C Program to print even and odd numbers in an array.

Problem Solution

1. Take size of array as input, N.
2. Enter N elements in the array.
3. Run a loop from 0 to N and in each iteration divide the number by 2.
4. Check the remainder generated.
5. Print all the numbers as even whose remainder generated is 0.
6. Print all the numbers as odd whose remainder generated is 1.

There are several ways to print even and odd numbers in an array in C language. Let’s take a detailed look at all the approaches to print even and odd numbers in an array.

Method 1: (Using if-else and Modulus Operator)

In this approach, we print even and odd numbers in an array using if-else statement and modulus operator.

Program/Source Code

Here is the source code of the C program to print all the even and odd numbers in an array using if-else statement and modulus operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to print even and odd numbers in an array using if-else and modulus operator`
3. ` */`
4. ` `
5. `#include <stdio.h>`
6. `void main() `
7. `{`
8. `    int n;`
9. `    printf("Enter number of elements in the array: ");`
10. `    scanf("%d", &n);`
11. ` `
12. `    int arr[n];`
13. ` `
14. `    //Take n elements as input from the user`
15. `    printf("Enter %d elements in the array: ",n);`
16. `    for(int i=0;i<n;i++)`
17. `    {`
18. `        scanf("%d",&arr[i]);`
19. `    }`
20. ` `
21. `    //Print all the even numbers`
22. `    printf("Even numbers in the array are: ");`
23. `    for(int i=0;i<n;i++)`
24. `    {`
25. `        if(arr[i]%2==0)`
26. `            printf("%d ", arr[i]);`
27. `    }`
28. ` `
29. `    //print all the odd numbers`
30. `    printf("\nOdd numbers in the array are: ");`
31. `    for(int i=0;i<n;i++)`
32. `    {`
33. `        if(arr[i]%2==1)`
34. `            printf("%d ", arr[i]);`
35. `    }`
36. `}`
Program Explanation

1. Enter number of elements in the array, n.
2. Take n elements as input from the user and store it in the array arr[].
3. For printing the even numbers in the array, run a loop from 0 to n and in each loop check if arr[i] %2 = 0 or not. If it is equal to 0 print it under even numbers.
4. For printing the odd numbers in the array, run a loop from 0 to n and in each loop check if arr[i] %2 = 1 or not. If it is equal to 1 print it under odd numbers.

Example:

Participate in C Programming Certification Contest of the Month Now!
• Let the number of elements in the array are 6 and the array elements are 12, 19, 45, 69, 98, 23.
• For printing even numbers run a loop from 0 to n i.e., 6.
• In each iteration check if the value in the array when divided by 2 leaves a remainder as 0 or not. If the condition satisfies print it under even numbers.
• So, even numbers in the array after complete iteration are 12, 98.
• For printing odd numbers run a loop from 0 to n i.e., 6.
• In each iteration check if the value in the array when divided by 2 leaves a remainder as 1 or not. If the condition satisfies print it under odd numbers.
• So, odd numbers in the array after complete iteration are 19, 45, 69, 23.

Time Complexity: O(n)
The above program for checking whether a number is even or odd has a time complexity of O(n), as the for loop runs from 0 to n.

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the numbers.

Runtime Test Cases

Here is the runtime output of a C program to print even and odd numbers in an array when the user enters the number of elements in the array as “6” and the elements of the array as “12, 19, 45, 69, 98, and 23”.

```Enter number of elements in the array: 6
Enter 6 elements in the array: 12 19 45 69 98 23
Even numbers in the array are: 12 98
Odd numbers in the array are: 19 45 69 23```

Method 2: (Using Bitwise Operator)

In this approach, we print even and odd numbers in an array using bitwise operator.

Program/Source Code

Here is the source code of the C program to print all the even and odd numbers in an array using bitwise operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C program to print even and odd numbers in an array using bitwise operator.`
3. `*/`
4. ` `
5. `#include <stdio.h>`
6. `void main()`
7. `{`
8. `    int n;`
9. `    printf("Enter number of elements in the array: ");`
10. `    scanf("%d", &n);`
11. ` `
12. `    int arr[n];`
13. ` `
14. `    //Take n elements as input from the user`
15. `    printf("Enter %d elements in the array: ",n);`
16. `    for(int i=0;i<n;i++)`
17. `    {`
18. `        scanf("%d",&arr[i]);`
19. `    }`
20. ` `
21. `    //Print all the even numbers`
22. `    printf("Even numbers in the array are: ");`
23. `    for(int i=0;i<n;i++)`
24. `    {`
25. `        if(arr[i]&1==1);`
26. `        else`
27. `            printf("%d ", arr[i]);`
28. `    }`
29. ` `
30. `    //Print all the odd numbers`
31. `    printf("\nOdd numbers in the array are: ");`
32. `    for(int i=0;i<n;i++)`
33. `    {`
34. `        if(arr[i]&1==1)`
35. `            printf("%d ", arr[i]);`
36. `    }`
37. `}`
Program Explanation

1. Enter number of elements in the array, n.
2. Take n elements as input from the user and store it in the array arr[].
3. For printing the odd numbers in the array, run a loop from 0 to n and in each loop check if arr[i] &1 = 1 or not. If it is equal to 1 print it under odd numbers.
4. For printing the even numbers in the array, run a loop from 0 to n and in each loop check if arr[i] &1 = 1 or not. If it is not equal equal to 1 print it under even numbers.

Example:

• Let the number of elements in the array are 6 and the array elements are 4, 19, 45, 69, 98, 23.
• For printing even numbers run a loop from 0 to n i.e., 6. In each iteration check if arr[i]&1 = 1 or not.
• If the condition does not satisfy i.e., arr[i]&1 is not equal to 1 print it under even numbers.
• So, even numbers in the array after complete iteration are 4, 98.
• For printing odd numbers run a loop from 0 to n i.e., 6. In each iteration check if arr[i] & 1=1 or not.
• If the condition satisfies print it under odd numbers.
• So, odd numbers in the array after complete iteration are 19, 45, 69, 23.

How bitwise and works:
Bitwise operator works with binary numbers. Therefore, it will convert numbers to binary and then perform the operation. Consider the first number 4.

• 4 in binary = 00000100
• 1 in binary = 00000001

Value after bitwise & = 00000000
Since, the value is not equal to 1 print 4 under even numbers.

Time Complexity: O(n)
The above program for checking whether a number is even or odd has a time complexity of O(n), as the for loop runs from 0 to n.

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the numbers.

Runtime Test Cases

Here is the runtime output of a C program to print even and odd numbers in an array when the user enters the number of elements in the array as “6” and the elements of the array as “4, 19, 45, 69, 98, and 23”.

```Enter number of elements in the array: 6
Enter 6 elements in the array: 4 19 45 69 98 23
Even numbers in the array are: 4 98
Odd numbers in the array are: 19 45 69 23```

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.