**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

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

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.

- Print Even and Odd Numbers in an array in C using if-else and Modulus Operator
- Print Even and Odd Numbers in an array in C using Bitwise Operator

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

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.

`/*`

`* C program to print even and odd numbers in an array using if-else and modulus operator`

`*/`

`#include <stdio.h>`

void main()

`{`

int n;

printf("Enter number of elements in the array: ");

scanf("%d", &n);

int arr[n];

`//Take n elements as input from the user`

printf("Enter %d elements in the array: ",n);

for(int i=0;i<n;i++)

`{`

scanf("%d",&arr[i]);

`}`

`//Print all the even numbers`

printf("Even numbers in the array are: ");

for(int i=0;i<n;i++)

`{`

if(arr[i]%2==0)

printf("%d ", arr[i]);

`}`

`//print all the odd numbers`

printf("\nOdd numbers in the array are: ");

for(int i=0;i<n;i++)

`{`

if(arr[i]%2==1)

printf("%d ", arr[i]);

`}`

`}`

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:**

- 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.

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

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

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.

`/*`

`* C program to print even and odd numbers in an array using bitwise operator.`

`*/`

`#include <stdio.h>`

void main()

`{`

int n;

printf("Enter number of elements in the array: ");

scanf("%d", &n);

int arr[n];

`//Take n elements as input from the user`

printf("Enter %d elements in the array: ",n);

for(int i=0;i<n;i++)

`{`

scanf("%d",&arr[i]);

`}`

`//Print all the even numbers`

printf("Even numbers in the array are: ");

for(int i=0;i<n;i++)

`{`

if(arr[i]&1==1);

`else`

printf("%d ", arr[i]);

`}`

`//Print all the odd numbers`

printf("\nOdd numbers in the array are: ");

for(int i=0;i<n;i++)

`{`

if(arr[i]&1==1)

printf("%d ", arr[i]);

`}`

`}`

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.

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”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check C Books