w3resource

C Exercises: Separate odd and even integers in separate arrays

C Array: Exercise-10 with Solution

Write a program in C to separate odd and even integers into separate arrays.

Pictorial Presentation:

C Exercises: Separate odd and even integers in separate arrays

Sample Solution:

C Code:

#include <stdio.h>

void main()
 {
    int arr1[10], arr2[10], arr3[10];
    int i,j=0,k=0,n;
	
	
       printf("\n\nSeparate odd and even integers in separate arrays:\n");
       printf("------------------------------------------------------\n");	

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);
   
       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }

    for(i=0;i<n;i++)
    {
	if (arr1[i]%2 == 0)
	{
	   arr2[j] = arr1[i];
	   j++;
	}
	else
	{
	   arr3[k] = arr1[i];
	   k++;
	}
    }

    printf("\nThe Even elements are : \n");
    for(i=0;i<j;i++)
    {
	printf("%d ",arr2[i]);
    }

    printf("\nThe Odd elements are :\n");
    for(i=0;i<k;i++)
    {
	printf("%d ", arr3[i]);
    }
    printf("\n\n");	
 }

Sample Output:

Separate odd and even integers in separate arrays:                                                            
------------------------------------------------------                                                        
Input the number of elements to be stored in the array :5                                                     
Input 5 elements in the array :                                                                               
element - 0 : 25                                                                                              
element - 1 : 47                                                                                              
element - 2 : 42                                                                                              
element - 3 : 56                                                                                              
element - 4 : 32                                                                                              
                                                                                                              
The Even elements are :                                                                                       
42 56 32                                                                                                      
The Odd elements are :                                                                                        
25 47

Flowchart:

Flowchart: Separate odd and even integers in separate arrays

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to find the maximum and minimum element in an array.
Next: Write a program in C to sort elements of array in ascending order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP