Count even and odd digits in an Integer
A certain number is given and the task is to count even digits and odd digits of the number and also of even digits are present even 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++
// C++ program to count // even and odd digits // in a given number #include <iostream> using namespace std; // Function to count digits 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; } 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 Code int main() { int n; n = 2335453; int t = countEvenOdd(n); if (t == 1) cout << "\nYES" << endl; else cout << "\nNO" << endl; return 0; } |
Java
// Java program to count // even and odd digits // in a given number import java.io.*; class GFG { // Function to count digits static 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") ; } } |
Python3
# python program to count even and # odd digits in a given number # Function to count digits def 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 # Drive code n = 2335453; t = countEvenOdd(n); if (t == 1): print("YES") else: print("NO") # This code is contributed by Sam007. |
C#
// C# program to count even and // odd digits in a given number using System; class GFG { // Function to count digits static 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 // PHP program to count // even and odd digits // in a given number // Function to count digits function 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. ?> |
Output :
Even count : 2 Odd count : 5 YES
Another solution to solve this problem using character array or string.
C++
// 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 digits 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++; } 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 Code int 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
// Java program to count // even and odd digits // in a given number // using char array import java.io.*; 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++; } 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
# Python3 program to count # even and odd digits # in a given number # using char array # Function to count digits def countEvenOdd(num, n): even_count = 0; odd_count = 0; for i in range (1, n + 1): x = num[i - 1]; if x % 2 == 0: even_count = even_count + 1; else: odd_count = 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 Code num = (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#
// C# program to count // even and odd digits // in a given number // using char array using 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 // PHP program to count // even and odd digits // in a given number // using char array // Function to count digits function 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. ?> |
Output :
Even count : 1 Odd count : 2 NO
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 contribute@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.
Recommended Posts:
- Program to count digits in an integer (4 Different Methods)
- Print first k digits of 1/n where n is a positive integer
- Reverse digits of an integer with overflow handled
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count the number of holes in an integer
- Count numbers with same first and last digits
- Count digits in a factorial | Set 1
- Count digits in a factorial | Set 2
- Count total number of digits from 1 to n
- Count digits in given number N which divide N
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count distinct pairs from two arrays having same sum of digits
- Count of n digit numbers whose sum of digits equals to given sum
- Count Numbers with N digits which consists of even number of 0’s
- Count different numbers that can be generated such that there digits sum is equal to 'n'



