C Program to Arrange the Odd Digits and Even Digits in Order

Write a program to arrange the odd digits and even digits in a given number

Input Format
The input contains an integer and option
If option = 0, even digits should come first
If option = 1, odd digits should come first

Output Format
Print the number after seggregation

Constraint
1<=Input<=10^7 
 Sample Input 1

67854 0

Sample Output 1

68475

Sample Input 2

12345 1

Sample Output 2

13524

Code:

#include<stdio.h>
int main()
{
    int n,d;
    int i=0;
    int a[100],odd[100],even[100];
    scanf("%d%d",&n,&d);
    while(n)
    {
        a[i++]=n%10;
        n=n/10;
    }
    int k=0,l=0;
    for(int j=0;j<i;j++)
    {
        if(a[j] %2 == 0)
        {
            even[k++]=a[j];
        }
        else
        odd[l++]=a[j];
    }
    if(d == 0)
    {
        for(int j=k-1;j>=0;j--) printf("%d",even[j]);
        for(int j=l-1;j>=0;j--) printf("%d",odd[j]);
    }
    else
    {
        
        
        for(int j=l-1;j>=0;j--) printf("%d",odd[j]);
        for(int j=k-1;j>=0;j--) printf("%d",even[j]);
    
    }
}
Code language: C/AL (cal)

Leave a Comment