Given an integer **K** and a range of consecutive numbers **[L, R]**. The task is to count the numbers from the given range which have digital root as K (1 ≤ K ≤ 9). Digital root is sum of digits of a number until it becomes a single digit number. For example, 256 -> 2 + 5 + 6 = 13 -> 1 + 3 = 4.

**Examples:**

Input:L = 10, R = 22, K = 3

Output:2

12 and 21 are the only numbers from the range whose digit sum is 3.

Input:L = 100, R = 200, K = 5

Output:11

**Approach:**

- First thing is to note that for any number Sum of Digits is equal to Number % 9. If remainder is 0, then sum of digits is 9.
- So if K = 9, then replace K with 0.
- Task, now is to find count of numbers in range L to R with modulo 9 equal to K.
- Divide the entire range into the maximum possible groups of 9 starting with L (TotalRange / 9), since in each range there will be exactly one number with modulo 9 equal to K.
- Loop over rest number of elements from R to R – count of rest elements, and check if any number satisfies the condition.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `#define ll long long int ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of required numbers ` `int` `countNumbers(` `int` `L, ` `int` `R, ` `int` `K) ` `{ ` ` ` `if` `(K == 9) ` ` ` `K = 0; ` ` ` ` ` `// Count of numbers present ` ` ` `// in given range ` ` ` `int` `totalnumbers = R - L + 1; ` ` ` ` ` `// Number of groups of 9 elements ` ` ` `// starting from L ` ` ` `int` `factor9 = totalnumbers / 9; ` ` ` ` ` `// Left over elements not covered ` ` ` `// in factor 9 ` ` ` `int` `rem = totalnumbers % 9; ` ` ` ` ` `// One Number in each group of 9 ` ` ` `int` `ans = factor9; ` ` ` ` ` `// To check if any number in rem ` ` ` `// satisfy the property ` ` ` `for` `(` `int` `i = R; i > R - rem; i--) { ` ` ` `int` `rem1 = i % 9; ` ` ` `if` `(rem1 == K) ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `L = 10; ` ` ` `int` `R = 22; ` ` ` `int` `K = 3; ` ` ` `cout << countNumbers(L, R, K); ` ` ` ` ` `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, ` `int` `K) { ` ` ` `if` `(K == ` `9` `) { ` ` ` `K = ` `0` `; ` ` ` `} ` ` ` ` ` `// Count of numbers present ` ` ` `// in given range ` ` ` `int` `totalnumbers = R - L + ` `1` `; ` ` ` ` ` `// Number of groups of 9 elements ` ` ` `// starting from L ` ` ` `int` `factor9 = totalnumbers / ` `9` `; ` ` ` ` ` `// Left over elements not covered ` ` ` `// in factor 9 ` ` ` `int` `rem = totalnumbers % ` `9` `; ` ` ` ` ` `// One Number in each group of 9 ` ` ` `int` `ans = factor9; ` ` ` ` ` `// To check if any number in rem ` ` ` `// satisfy the property ` ` ` `for` `(` `int` `i = R; i > R - rem; i--) { ` ` ` `int` `rem1 = i % ` `9` `; ` ` ` `if` `(rem1 == K) { ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `L = ` `10` `; ` ` ` `int` `R = ` `22` `; ` ` ` `int` `K = ` `3` `; ` ` ` `System.out.println(countNumbers(L, R, K)); ` ` ` `} ` `} ` `/* This code 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, K): ` ` ` ` ` `if` `(K ` `=` `=` `9` `): ` ` ` `K ` `=` `0` ` ` ` ` `# Count of numbers present ` ` ` `# in given range ` ` ` `totalnumbers ` `=` `R ` `-` `L ` `+` `1` ` ` ` ` `# Number of groups of 9 elements ` ` ` `# starting from L ` ` ` `factor9 ` `=` `totalnumbers ` `/` `/` `9` ` ` ` ` `# Left over elements not covered ` ` ` `# in factor 9 ` ` ` `rem ` `=` `totalnumbers ` `%` `9` ` ` ` ` `# One Number in each group of 9 ` ` ` `ans ` `=` `factor9 ` ` ` ` ` `# To check if any number in rem ` ` ` `# satisfy the property ` ` ` `for` `i ` `in` `range` `(R, R ` `-` `rem, ` `-` `1` `): ` ` ` `rem1 ` `=` `i ` `%` `9` ` ` `if` `(rem1 ` `=` `=` `K): ` ` ` `ans ` `+` `=` `1` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `L ` `=` `10` `R ` `=` `22` `K ` `=` `3` `print` `(countNumbers(L, R, K)) ` ` ` `# This code is contributed ` `# by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System ; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the count ` ` ` `// of required numbers ` ` ` `static` `int` `countNumbers(` `int` `L, ` `int` `R, ` `int` `K) ` ` ` `{ ` ` ` `if` `(K == 9) ` ` ` `{ ` ` ` `K = 0; ` ` ` `} ` ` ` ` ` `// Count of numbers present ` ` ` `// in given range ` ` ` `int` `totalnumbers = R - L + 1; ` ` ` ` ` `// Number of groups of 9 elements ` ` ` `// starting from L ` ` ` `int` `factor9 = totalnumbers / 9; ` ` ` ` ` `// Left over elements not covered ` ` ` `// in factor 9 ` ` ` `int` `rem = totalnumbers % 9; ` ` ` ` ` `// One Number in each group of 9 ` ` ` `int` `ans = factor9; ` ` ` ` ` `// To check if any number in rem ` ` ` `// satisfy the property ` ` ` `for` `(` `int` `i = R; i > R - rem; i--) ` ` ` `{ ` ` ` `int` `rem1 = i % 9; ` ` ` `if` `(rem1 == K) ` ` ` `{ ` ` ` `ans++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `L = 10; ` ` ` `int` `R = 22; ` ` ` `int` `K = 3; ` ` ` ` ` `Console.WriteLine(countNumbers(L, R, K)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Ryuga */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count ` `// of required numbers ` `function` `countNumbers(` `$L` `, ` `$R` `, ` `$K` `) ` `{ ` ` ` `if` `(` `$K` `== 9) ` ` ` `$K` `= 0; ` ` ` ` ` `// Count of numbers present ` ` ` `// in given range ` ` ` `$totalnumbers` `= ` `$R` `- ` `$L` `+ 1; ` ` ` ` ` `// Number of groups of 9 elements ` ` ` `// starting from L ` ` ` `$factor9` `= ` `intval` `(` `$totalnumbers` `/ 9); ` ` ` ` ` `// Left over elements not covered ` ` ` `// in factor 9 ` ` ` `$rem` `= ` `$totalnumbers` `% 9; ` ` ` ` ` `// One Number in each group of 9 ` ` ` `$ans` `= ` `$factor9` `; ` ` ` ` ` `// To check if any number in rem ` ` ` `// satisfy the property ` ` ` `for` `(` `$i` `= ` `$R` `; ` `$i` `> ` `$R` `- ` `$rem` `; ` `$i` `--) ` ` ` `{ ` ` ` `$rem1` `= ` `$i` `% 9; ` ` ` `if` `(` `$rem1` `== ` `$K` `) ` ` ` `$ans` `++; ` ` ` `} ` ` ` ` ` `return` `$ans` `; ` `} ` ` ` `// Driver code ` `$L` `= 10; ` `$R` `= 22; ` `$K` `= 3; ` `echo` `countNumbers(` `$L` `, ` `$R` `, ` `$K` `); ` ` ` `// This code is contributed by Ita_c ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

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:

- Digital Root (repeated digital sum) of the given large integer
- Digital Root of a given large integer using Recursion
- Sudo Placement[1.7] | Greatest Digital Root
- Print a number containing K digits with digital root D
- Find Nth positive number whose digital root is X
- Count numbers from a given range that contains a given number as the suffix
- Print all Good numbers in given range
- Count numbers with unit digit k in given range
- Count of numbers with all digits same in a given range
- Cumulative product of digits of all numbers in the given range
- Print all numbers in given range having digits in strictly increasing order
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Find the highest occurring digit in prime numbers in a range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Total numbers with no repeated digits in a range
- Count Unary Numbers in a Range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Range Queries for count of Armstrong numbers in subarray using MO's algorithm

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.