Write a 'C' program to arrange the even digits first and odd digits second of the given number vice versa.
Input Format
First input is option and next the value If the option is 0 - odd digits first 1 - even digits first
Constraints
1 ≤ num ≤ 100000007
Output Format
print the value
Sample Input 0
0 89745
Sample Output 0
97584
Answers
This program segment given below uses a straight-forward approach to count the number of odd and even digits in a given integer number (number).
Initially, variables n odd and n even, the counts for odd and even digits, respectively, are initialized to zero. Then a while loop is used to count the odd and even digits. In each iteration of this loop, the least significant digit of number number is first determined in variable digit,then the appropriate counter is incremented using an if-else statement and the least significant digit is removed from number number. The loop is executed as long as the value of number is greater than zero, i. e., all digits in number are not processed. Finally, the values of n odd and never are printed.
#include <stdio.h>
void main()
{
int n odd, n even, num,digit ;
clr scr ();
print f("Count number of odd and even digits in a given integer number ");
scan f("%d",& num);
n odd = n even =0; /* count of odd and even digits */
while ( num> 0)
{
digit = num % 10; /* separate LS digit from number */
if (digit % 2 == 1)
nodd++;
else n even++;
num /= 10; /* remove LS digit from num */
}
print f("Odd digits : %d Even digits: %d\n", n odd, n even);
getch (); }