# Number of even substrings in a string of digits

Given a string of digits 0 – 9. The task is to count number of substrings which when convert into integer form an even number.

Examples :

```Input : str = "1234".
Output : 6
"2", "4", "12", "34", "234", "1234"
are 6 substring which are even.

Input : str = "154".
Output : 3

Input : str = "15".
Output : 0
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

For a number to be even, substring must end wit an even digit. We find all the even digits in the string and for each even digit, count the number of substrings ending with it. Now, observe that the number of substrings will be index of that even digit plus one.

## C++

 `// C++ program to count number of substring ` `// which are even integer in a string of digits. ` `#include ` `using` `namespace` `std; ` ` `  `// Return the even number substrings. ` `int` `evenNumSubstring(``char` `str[]) ` `{ ` `    ``int` `len = ``strlen``(str); ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < len; i++) ` `    ``{ ` `        ``int` `temp = str[i] - ``'0'``; ` ` `  `        ``// If current digit is even, add ` `        ``// count of substrings ending with ` `        ``// it. The count is (i+1) ` `        ``if` `(temp % 2 == 0) ` `            ``count += (i + 1); ` `    ``} ` ` `  `    ``return` `count; ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``char` `str[] = ``"1234"``; ` `    ``cout << evenNumSubstring(str) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to count number of ` `// substring which are even integer ` `// in a string of digits. ` `public` `class` `GFG { ` `     `  `    ``// Return the even number substrings. ` `    ``static` `int` `evenNumSubstring(String str) ` `    ``{ ` `        ``int` `len = str.length(); ` `        ``int` `count = ``0``; ` `     `  `        ``for` `(``int` `i = ``0``; i < len; i++) ` `        ``{ ` `            ``int` `temp = str.charAt(i) - ``'0'``; ` `     `  `            ``// If current digit is even, add ` `            ``// count of substrings ending with ` `            ``// it. The count is (i+1) ` `            ``if` `(temp % ``2` `== ``0``) ` `                ``count += (i + ``1``); ` `        ``} ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `         `  `        ``String str= ``"1234"``; ` `         `  `        ``System.out.println(evenNumSubstring(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## Python3

 `# Python 3 program to count number of substring ` `# which are even integer in a string of digits. ` ` `  ` `  `# Return the even number substrings. ` `def` `evenNumSubstring(``str``): ` `    ``length ``=` `len``(``str``) ` `    ``count ``=` `0` ` `  `    ``for` `i ``in` `range``(``0``,length,``1``): ` `        ``temp ``=` `ord``(``str``[i]) ``-` `ord``(``'0'``) ` ` `  `        ``# If current digit is even, add ` `        ``# count of substrings ending with ` `        ``# it. The count is (i+1) ` `        ``if` `(temp ``%` `2` `=``=` `0``): ` `            ``count ``+``=` `(i ``+` `1``) ` ` `  `    ``return` `count ` ` `  `# Driven Program ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``str` `=` `[``'1'``,``'2'``,``'3'``,``'4'``] ` `    ``print``(evenNumSubstring(``str``)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to count number of ` `// substring which are even integer ` `// in a string of digits. ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``// Return the even number substrings. ` `    ``static` `int` `evenNumSubstring(``string` `str) ` `    ``{ ` `        ``int` `len = str.Length; ` `        ``int` `count = 0; ` `     `  `        ``for` `(``int` `i = 0; i < len; i++) ` `        ``{ ` `            ``int` `temp = str[i] - ``'0'``; ` `     `  `            ``// If current digit is even, ` `            ``// add count of substrings  ` `            ``// ending with it. The count ` `            ``// is (i+1) ` `            ``if` `(temp % 2 == 0) ` `                ``count += (i + 1); ` `        ``} ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `str= ``"1234"``; ` `     `  `        ``Console.Write( ` `            ``evenNumSubstring(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output:

```6
```

Time Complexity: O(length of string).

This article is contributed by Anuj Chauhan. 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : Sam007, jit_t, SURENDRA_GANGWAR

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.