Count even and odd digits in an Integer - GeeksforGeeks

28 Feb, 2022
A certain number is given and the task is to count even digits and odd digits of the number and also even digits are present even a number of times and, similarly, for odd numbers. 
Print Yes If:
   If number contains even digits even number of time
   Odd digits odd number of times
Else 
   Print No
Examples :
Input : 22233
Output : NO
         count_even_digits = 3
         count_odd_digits = 2
         In this number even digits occur odd number of times and odd 
         digits occur even number of times so its print NO.

Input : 44555
Output : YES
        count_even_digits = 2
        count_odd_digits = 3
        In this number even digits occur even number of times and odd 
        digits occur odd number of times so its print YES.
Efficient solution for calculating even and odd digits in a number.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count// even and odd digits// in a given number#include <iostream>using namespace std; // Function to count digitsint countEvenOdd(int n){    int even_count = 0;    int odd_count = 0;    while (n > 0)    {        int rem = n % 10;        if (rem % 2 == 0)            even_count++;        else            odd_count++;        n = n / 10;    }    cout << "Even count : "         << even_count;    cout << "\nOdd count : "         << odd_count;    if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;} // Driver Codeint main(){    int n;    n = 2335453;    int t = countEvenOdd(n);    if (t == 1)        cout << "\nYES" << endl;    else        cout << "\nNO" << endl;    return 0;}
// Java program to count// even and odd digits// in a given number import java.io.*; class GFG{     // Function to count digitsstatic int countEvenOdd(int n){    int even_count = 0;    int odd_count = 0;    while (n > 0)    {        int rem = n % 10;        if (rem % 2 == 0)            even_count++;        else            odd_count++;        n = n / 10;    }    System.out.println ( "Even count : " +                              even_count);    System.out.println ( "Odd count : " +                              odd_count);    if (even_count % 2 == 0 &&         odd_count % 2 != 0)        return 1;    else        return 0;}     // Driver Code    public static void main (String[] args)    {    int n;    n = 2335453;    int t = countEvenOdd(n);         if (t == 1)        System.out.println ( "YES" );    else        System.out.println( "NO") ;             }}
# python program to count even and# odd digits in a given number # Function to count digitsdef countEvenOdd(n):         even_count = 0    odd_count = 0    while (n > 0):        rem = n % 10        if (rem % 2 == 0):            even_count += 1        else:            odd_count += 1                     n = int(n / 10)         print( "Even count : " , even_count)    print("\nOdd count : " , odd_count)    if (even_count % 2 == 0 and                    odd_count % 2 != 0):        return 1    else:        return 0 # Driver coden = 2335453;t = countEvenOdd(n); if (t == 1):    print("YES")else:    print("NO") # This code is contributed by Sam007.
// C# program to count even and// odd digits in a given numberusing System; class GFG {     // Function to count digitsstatic int countEvenOdd(int n){    int even_count = 0;    int odd_count = 0;    while (n > 0) {        int rem = n % 10;        if (rem % 2 == 0)            even_count++;        else            odd_count++;        n = n / 10;    }         Console.WriteLine("Even count : " +                       even_count);    Console.WriteLine("Odd count : " +                       odd_count);    if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;}     // Driver Code    public static void Main ()    {            int n;            n = 2335453;            int t = countEvenOdd(n);            if (t == 1)                Console.WriteLine ("YES");            else                Console.WriteLine("NO") ;                 }} // This code is contributed by vt_m.
<?php// PHP program to count// even and odd digits// in a given number // Function to count digitsfunction countEvenOdd($n){    $even_count = 0;    $odd_count = 0;    while ($n > 0)    {        $rem = $n % 10;        if ($rem % 2 == 0)            $even_count++;        else            $odd_count++;        $n = (int)($n / 10);    }    echo("Even count : " .             $even_count);    echo("\nOdd count : " .               $odd_count);    if ($even_count % 2 == 0 &&        $odd_count % 2 != 0)        return 1;    else        return 0;} // Driver code$n = 2335453;$t = countEvenOdd($n);if ($t == 1)    echo("\nYES");else    echo("\nNO"); // This code is contributed by Ajit.?>
<script> /// Javascript program to count// even and odd digits// in a given number // Function to count digitsfunction countEvenOdd(n){    let even_count = 0;    let odd_count = 0;    while (n > 0)    {        let rem = n % 10;        if (rem % 2 == 0)            even_count++;        else            odd_count++;        n = Math.floor(n / 10);    }    document.write("Even count : "        + even_count);    document.write("<br>" + "Odd count : "        + odd_count);    if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;} // Driver Code     let n;    n = 2335453;    let t = countEvenOdd(n);    if (t == 1)        document.write("<br>" + "YES" + "<br>");    else        document.write("<br>"+"NO" + "<br>"); // This code is contributed by Mayank Tyagi </script>
Even count : 2
Odd count : 5
YES
Another solution to solve this problem is a character array or string.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count// even and odd digits// in a given number// using char array#include <bits/stdc++.h>using namespace std; // Function to count digitsint countEvenOdd(char num[],                 int n){    int even_count = 0;    int odd_count = 0;    for (int i = 0; i < n; i++)    {        int x = num[i] - 48;        if (x % 2 == 0)            even_count++;        else            odd_count++;    }    cout << "Even count : "         << even_count;    cout << "\nOdd count : "         << odd_count;     if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;} // Driver Codeint main(){    char num[18] = { 1, 2, 3 };     int n = strlen(num);    int t = countEvenOdd(num, n);    if (t == 1)        cout << "\nYES" << endl;    else        cout << "\nNO" << endl;    return 0;}
// Java program to count// even and odd digits// in a given number// using char array import java.io.*;  class GFG{     // Function to count digitsstatic int countEvenOdd(char num[],                        int n){    int even_count = 0;    int odd_count = 0;    for (int i = 0; i < n; i++)    {        int x = num[i] - 48;        if (x % 2 == 0)            even_count++;        else            odd_count++;    }     System.out.println ("Even count : " +                         even_count);    System.out.println( "Odd count : " +                         odd_count);     if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;}     // Driver Code    public static void main (String[] args)    {        char num[] = { 1, 2, 3 };     int n = num.length;    int t = countEvenOdd(num, n);    if (t == 1)        System.out.println("YES") ;    else        System.out.println("NO") ;    }} // This code is contributed by vt_m
# Python3 program to count# even and odd digits# in a given number# using char array # Function to count digitsdef countEvenOdd(num, n):    even_count = 0;    odd_count = 0;    num=list(str(num))    for i in num:        if i in ('0','2','4','6','8'):            even_count+=1        else:            odd_count+=1    print("Even count : ",              even_count);    print("Odd count : ",              odd_count);    if (even_count % 2 == 0 and        odd_count % 2 != 0):        return 1;    else:        return 0; # Driver Codenum = (1, 2, 3);n = len(num);t = countEvenOdd(num, n); if t == 1:    print("YES");else:    print("NO");     # This code is contributed by mits.
// C# program to count// even and odd digits// in a given number// using char arrayusing System; class GFG{         // Function to count digits    static int countEvenOdd(char []num,                            int n)    {        int even_count = 0;        int odd_count = 0;        for (int i = 0; i < n; i++)        {            int x = num[i] - 48;            if (x % 2 == 0)                even_count++;            else                odd_count++;        }             Console.WriteLine("Even count : " +                               even_count);                                     Console.WriteLine( "Odd count : " +                                odd_count);             if (even_count % 2 == 0 &&            odd_count % 2 != 0)            return 1;        else            return 0;    }     // Driver code    public static void Main ()    {        char [] num = { '1', '2', '3' };             int n = num.Length;        int t = countEvenOdd(num, n);                 if (t == 1)            Console.WriteLine("YES") ;        else            Console.WriteLine("NO") ;    }} // This code is contributed by Sam007.
<?php// PHP program to count// even and odd digits// in a given number// using char array  // Function to count digitsfunction countEvenOdd($num, $n){         $even_count = 0;    $odd_count = 0;    for ($i = 0; $i < $n; $i++)    {        $x = $num[$i] - 48;        if ($x % 2 == 0)            $even_count++;        else            $odd_count++;    }    echo "Even count : " .          $even_count;    echo "\nOdd count : " .            $odd_count;     if ($even_count % 2 == 0 &&        $odd_count % 2 != 0)        return 1;    else        return 0;}     // Driver Code    $num = array( 1, 2, 3 );     $n = strlen(num);    $t = countEvenOdd($num, $n);    if ($t == 1)        echo "\nYES" ,"\n";    else        echo "\nNO" ,"\n"; // This code is contributed by ajit.?>
<script> // Javascript program to count// even and odd digits// in a given number// using char array // Function to count digitsfunction countEvenOdd(num, n){    even_count = 0;    odd_count = 0;         for(var i = 0; i < n; i++)    {        x = num[i] - 48;        if (x % 2 == 0)            even_count++;        else            odd_count++;    }         document.write("Even count : " +                   even_count);    document.write("<br>Odd count : " +                   odd_count);      if (even_count % 2 == 0 &&        odd_count % 2 != 0)        return 1;    else        return 0;} // Driver codevar num = [ 1, 2, 3 ];n = num.length;t = countEvenOdd(num, n); if (t == 1)    document.write("<br>YES <br>");else    document.write("<br>NO <br>");     // This code is contributed by akshitsaxenaa09 </script>
Even count : 1
Odd count : 2
NO
We have to convert the given number to a string by taking a new variable.
Traverse the string, convert each element to an integer.
If the character(digit) is even, then the increased count
Else increment the odd count.
If the even count is even and the odd count is odd, then print Yes.
Else print no.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of above approach#include<bits/stdc++.h>using namespace std; string getResult(int n){   // Converting integer to String  string st = to_string(n);  int even_count = 0;  int odd_count = 0;   // Looping till length of String  for(int i = 0; i < st.length(); i++)  {    if ((st[i] % 2) == 0)       // Digit is even so increment even count      even_count += 1;    else      odd_count += 1;  }   // Checking even count is even and  // odd count is odd  if (even_count % 2 == 0 &&      odd_count % 2 != 0)    return "Yes";  else    return "no";} // Driver Codeint main(){   int n = 77788;   // Passing this number to get result function  cout<<getResult(n)<<endl; } // This code is contributed by shinjanpatra.
// Java implementation of above approachclass GFG{     static String getResult(int n){         // Converting integer to String    String st = String.valueOf(n);    int even_count = 0;    int odd_count = 0;         // Looping  till length of String    for(int i = 0; i < st.length(); i++)    {        if ((st.charAt(i) % 2) == 0)                     // Digit is even so increment even count            even_count += 1;        else            odd_count += 1;    }         // Checking even count is even and    // odd count is odd    if (even_count % 2 == 0 &&         odd_count % 2 != 0)        return "Yes";    else        return "no";} // Driver Codepublic static void main(String[] args){    int n = 77788;         // Passing this number to get result function    System.out.println(getResult(n)); }} // This code is contributed by 29AjayKumar
# Python implementation of above approachdef getResult(n):       # Converting integer to string    st = str(n)    even_count = 0    odd_count = 0         # Looping  till length of string    for i in range(len(st)):               if((int(st[i]) % 2) == 0):                         # digit is even so increment even count            even_count += 1        else:            odd_count += 1     # Checking even count is even and odd count is odd    if(even_count % 2 == 0 and odd_count % 2 != 0):        return 'Yes'    else:        return 'no'  # Driver Coden = 77788 # passing this number to get result functionprint(getResult(n)) # this code is contributed by vikkycirus
// C# implementation of above approachusing System;using System.Collections.Generic; class GFG{     static String getResult(int n){         // Converting integer to String    String st = String.Join("",n);    int even_count = 0;    int odd_count = 0;         // Looping  till length of String    for(int i = 0; i < st.Length; i++)    {        if ((st[i] % 2) == 0)                     // Digit is even so increment even count            even_count += 1;        else            odd_count += 1;    }         // Checking even count is even and    // odd count is odd    if (even_count % 2 == 0 &&         odd_count % 2 != 0)        return "Yes";    else        return "no";} // Driver Codepublic static void Main(String[] args){    int n = 77788;         // Passing this number to get result function    Console.WriteLine(getResult(n)); }} // This code is contributed by Princi Singh
<script> // Javascript implementation of above approach  function getResult(n){         // Converting integer to String    var st = n.toString();    var even_count = 0;    var odd_count = 0;         // Looping  till length of String    for(var i = 0; i < st.length; i++)    {        if ((st.charAt(i) % 2) == 0)                     // Digit is even so increment even count            even_count += 1;        else            odd_count += 1;    }         // Checking even count is even and    // odd count is odd    if (even_count % 2 == 0 &&         odd_count % 2 != 0)        return "Yes";    else        return "no";} // Driver Code     var n = 77788;         // Passing this number to get result function    document.write(getResult(n));     </script>
Yes
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
vt_m
jit_t
Sam007
Mithun Kumar
geethanjalivilluri
Akanksha_Rai
mayanktyagi1709
vikkycirus
akshitsaxenaa09
29AjayKumar
bunnyram19
princi singh
shinjanpatra
number-digits
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Overriding in Java
Constructors in C++
Operator Overloading in C++
C++ Classes and Objects
Copy Constructor in C++
Types of Operating Systems
Interfaces in Java
Polymorphism in C++
C++ Data Types
Friend class and function in C++