# Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3

Given two integers **L** and **R**. The task is to find the count of all even numbers in the range **[L, R]** whose sum of digits is divisible by 3.

**Examples:**

Input:L = 18, R = 36

Output:4

18, 24, 30, 36 are the only numbers in the range [18, 36] which are even and whose sum of digits is divisible by 3.

Input:L = 7, R = 11

Output:0

There is no number in the range [7, 11] which is even and whose sum of digits is divisible by 3.

**Naive approach:** Initialize **count = 0** and for every number in the range **[L, R]**, check if the number is divisible by 2 and sum of its digits is divisible by 3. If **yes** then increment the count. Print the **count** in the end.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// sum of digits of x ` `int` `sumOfDigits(` `int` `x) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(x != 0) { ` ` ` `sum += x % 10; ` ` ` `x = x / 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the count ` `// of required numbers ` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = l; i <= r; i++) { ` ` ` ` ` `// If i is divisible by 2 and ` ` ` `// sum of digits of i is divisible by 3 ` ` ` `if` `(i % 2 == 0 && sumOfDigits(i) % 3 == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 1000, r = 6000; ` ` ` `cout << countNumbers(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// sum of digits of x ` `static` `int` `sumOfDigits(` `int` `x) ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` `while` `(x != ` `0` `) ` ` ` `{ ` ` ` `sum += x % ` `10` `; ` ` ` `x = x / ` `10` `; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the count ` `// of required numbers ` `static` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `{ ` ` ` ` ` `// If i is divisible by 2 and ` ` ` `// sum of digits of i is divisible by 3 ` ` ` `if` `(i % ` `2` `== ` `0` `&& sumOfDigits(i) % ` `3` `== ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `l = ` `1000` `, r = ` `6000` `; ` ` ` `System.out.println(countNumbers(l, r)); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# python implementation of the approach ` ` ` `# Function to return the ` `# sum of digits of x ` `def` `sumOfDigits(x): ` ` ` `sum` `=` `0` ` ` `while` `x !` `=` `0` `: ` ` ` `sum` `+` `=` `x ` `%` `10` ` ` `x ` `=` `x` `/` `/` `10` ` ` `return` `sum` ` ` ` ` `# Function to return the count ` `# of required numbers ` `def` `countNumbers(l, r): ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `): ` ` ` ` ` `# If i is divisible by 2 and ` ` ` `# sum of digits of i is divisible by 3 ` ` ` `if` `i ` `%` `2` `=` `=` `0` `and` `sumOfDigits(i) ` `%` `3` `=` `=` `0` `: ` ` ` `count ` `+` `=` `1` ` ` `return` `count ` ` ` `# Driver code ` `l ` `=` `1000` `; r ` `=` `6000` `print` `(countNumbers(l, r)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the ` `// sum of digits of x ` `static` `int` `sumOfDigits(` `int` `x) ` `{ ` ` ` `int` `sum = 0; ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `sum += x % 10; ` ` ` `x = x / 10; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the count ` `// of required numbers ` `static` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `{ ` ` ` ` ` `// If i is divisible by 2 and ` ` ` `// sum of digits of i is divisible by 3 ` ` ` `if` `(i % 2 == 0 && sumOfDigits(i) % 3 == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `l = 1000, r = 6000; ` ` ` `Console.WriteLine(countNumbers(l, r)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the sum of ` `// digits of x ` `function` `sumOfDigits( ` `$x` `) ` `{ ` ` ` `$sum` `= 0; ` ` ` `while` `(` `$x` `!= 0) ` ` ` `{ ` ` ` `$sum` `+= ` `$x` `% 10; ` ` ` `$x` `= ` `$x` `/ 10; ` ` ` `} ` ` ` `return` `$sum` `; ` `} ` ` ` `// Function to return the count ` `// of required numbers ` `function` `countNumbers(` `$l` `, ` `$r` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= ` `$l` `; ` `$i` `<= ` `$r` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// If i is divisible by 2 and ` ` ` `// sum of digits of i is divisible by 3 ` ` ` `if` `(` `$i` `% 2 == 0 && ` ` ` `sumOfDigits(` `$i` `) % 3 == 0) ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$l` `= 1000; ` `$r` `= 6000; ` `echo` `countNumbers(` `$l` `, ` `$r` `); ` ` ` `// This code is contributed by princiraj1992 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

834

**Time Complexity:** O(r – l)

**Efficient approach:**

- We have to check that the number is divisible by 2.
- We have to check that the sum of digit is divisible by 3 which means that the number is divisible by 3.

So overall we have to check if a number is divisible by both 2 and 3, and since both 2 and 3 are co prime so we just have to check if a number is divisible by their product i.e. 6.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of required numbers ` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// Count of numbers in range ` ` ` `// which are divisible by 6 ` ` ` `return` `((r / 6) - (l - 1) / 6); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 1000, r = 6000; ` ` ` `cout << countNumbers(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of required numbers ` `static` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// Count of numbers in range ` ` ` `// which are divisible by 6 ` ` ` `return` `((r / ` `6` `) - (l - ` `1` `) / ` `6` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `l = ` `1000` `, r = ` `6000` `; ` ` ` `System.out.println(countNumbers(l, r)); ` `} ` `} ` ` ` `// This code is contributed by princiraj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of required numbers ` `def` `countNumbers(l, r) : ` ` ` ` ` `# Count of numbers in range ` ` ` `# which are divisible by 6 ` ` ` `return` `((r ` `/` `/` `6` `) ` `-` `(l ` `-` `1` `) ` `/` `/` `6` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l ` `=` `1000` `; r ` `=` `6000` `; ` ` ` `print` `(countNumbers(l, r)); ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the count ` `// of required numbers ` `static` `int` `countNumbers(` `int` `l, ` `int` `r) ` `{ ` ` ` ` ` `// Count of numbers in range ` ` ` `// which are divisible by 6 ` ` ` `return` `((r / 6) - (l - 1) / 6); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `l = 1000, r = 6000; ` ` ` `Console.WriteLine(countNumbers(l, r)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

834

**Time Complexity:** O(1)

## Recommended Posts:

- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Sum of all numbers divisible by 6 in a given range
- Numbers that are not divisible by any number in the range [2, 10]
- N digit numbers divisible by 5 formed from the M digits
- Count integers in a range which are divisible by their euler totient value
- Total numbers with no repeated digits in a range
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers with same first and last digits

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.