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

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count ` `// of required numbers ` `function` `countNumbers(` `$l` `, ` `$r` `) ` `{ ` ` ` ` ` `// Count of numbers in range ` ` ` `// which are divisible by 6 ` ` ` `return` `((int)(` `$r` `/ 6) - ` ` ` `(int)((` `$l` `- 1) / 6)); ` `} ` ` ` `// Driver code ` `$l` `= 1000; ` `$r` `= 6000; ` `echo` `(countNumbers(` `$l` `, ` `$r` `)); ` ` ` `// This code is contributed ` `// by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

834

**Time Complexity:** O(1)

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.

## 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 given range such that sum of even digits is greater than sum of odd digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count all prime numbers in a given range whose sum of digits is also prime
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count the numbers with N digits and whose suffix is divisible by K
- Check if the sum of digits of number is divisible by all of its digits
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count all N digit numbers whose digits are multiple of X
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Count N digits numbers with sum divisible by K
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Count of numbers whose sum of increasing powers of digits is equal to the number itself

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.