20_10

Write a C program to divide an array into two arrays of even and odd elements.

Source code:

 //Write a C program to divide an array into two arrays of even and odd elements.

#include <stdio.h>

int main() 

{

int j,i, array[5];

for(j=0;j<5;++j)

{

printf("Enter the value for %d: ",j);

scanf("%d",&array[j]);

}

int even[5], odd[5];

int e = 0 , d = 0; /* e and d is for counting number of even or odd) */  

for(i = 0; i< 5; i++) {

    if(array[i]%2 == 0) {

        even[e] = array[i];

        e++;

    }

    else {

         odd[d] = array[i];

         d++;

    }

}

printf("Even array: ");

for(i= 0; i< e; i++)

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

    

printf("\n");

printf("Odd array: ");

for(i= 0; i< d; i++)

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

return 0;

}

Output:

C program to divide an array into two arrays of even and odd elements


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...