Wednesday, December 4, 2019

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

#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) //Even or Odd testing
        {
           arr2[j] = arr1[i]; //Even ele to arr2
           j++;
        }
        else
        {
           arr3[k] = arr1[i]; //Odd ele to arr3
           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");    
 }
 

/*
Separate odd and even integers in separate arrays:
------------------------------------------------------
Input the number of elements to be stored in the array :10
Input 10 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
element - 5 : 6
element - 6 : 7
element - 7 : 8
element - 8 : 9
element - 9 : 10

The Even elements are :
2 4 6 8 10
The Odd elements are :
1 3 5 7 9
*/

No comments:

Post a Comment