Count of numbers from range [L, R] whose sum of digits is Y
Given an integer Y and a range [L, R], the task is to find the count of all numbers from the given range whose sum of digits is equal to Y.
Examples:
Input: L = 0, R = 11, Y = 2
Output: 2
2 -> 2
11 -> 1 + 1 = 2
Input: L = 500, R = 1000, Y = 6
Output: 3
Approach: Initialize count = 0 and for every number from the range, calculate sum of it’s digits. If the digit sum is equal to Y 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 <iostream> using namespace std; // Function to return the sum // of digits of n int sumOfDigits(int n) { int sum = 0; while (n != 0) { // Add the last digit // to the sum sum = sum + n % 10; // Remove the last digit n = n / 10; } // Return the sum of the digits return sum; } // Function to return the count of numbers // from the range [l, r] whose digit sum // is equal to y int countTheNumbers(int l, int r, int y) { // Initialise the count to zero int count = 0; // Loop for each number from the range for (int i = l; i <= r; i++) { // If digit sum = y if (sumOfDigits(i) == y) count++; } // Return the required count return count; } // Driver code int main() { int l = 0, r = 10, y = 1; cout << countTheNumbers(l, r, y); return 0; } |
Java
// Java implementation of the approach class GfG { // Function to return the sum // of digits of n static int sumOfDigits(int n) { int sum = 0; while (n != 0) { // Add the last digit // to the sum sum = sum + n % 10; // Remove the last digit n = n / 10; } // Return the sum of the digits return sum; } // Function to return the count of numbers // from the range [l, r] whose digit sum // is equal to y static int countTheNumbers(int l, int r, int y) { // Initialise the count to zero int count = 0; // Loop for each number from the range for (int i = l; i <= r; i++) { // If digit sum = y if (sumOfDigits(i) == y) count++; } // Return the required count return count; } // Driver code public static void main(String[] args) { int l = 0, r = 10, y = 1; System.out.println(countTheNumbers(l, r, y)); } } // This code is contributed by Prerna Saini |
Python3
# Python3 implementation of the approach # Function to return the sum # of digits of n def sumOfDigits(n): Sum = 0 while (n != 0): # Add the last digit to the Sum Sum = Sum + n % 10 # Remove the last digit n = n // 10 # Return the Sum of the digits return Sum # Function to return the count of numbers # from the range [l, r] whose digit Sum # is equal to y def countTheNumbers(l, r, y): # Initialise the count to zero count = 0 # Loop for each number from the range for i in range(l, r + 1): # If digit sum = y if (sumOfDigits(i) == y): count += 1 # Return the required count return count # Driver code l, r, y = 0, 10, 1print(countTheNumbers(l, r, y)) # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GfG { // Function to return the sum // of digits of n static int sumOfDigits(int n) { int sum = 0; while (n != 0) { // Add the last digit // to the sum sum = sum + n % 10; // Remove the last digit n = n / 10; } // Return the sum of the digits return sum; } // Function to return the count of numbers // from the range [l, r] whose digit sum // is equal to y static int countTheNumbers(int l, int r, int y) { // Initialise the count to zero int count = 0; // Loop for each number from the range for (int i = l; i <= r; i++) { // If digit sum = y if (sumOfDigits(i) == y) count++; } // Return the required count return count; } // Driver code public static void Main(String[] args) { int l = 0, r = 10, y = 1; Console.WriteLine(countTheNumbers(l, r, y)); } } // This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the approach // Function to return the sum // of digits of n function sumOfDigits($n) { $sum = 0; while ($n != 0) { // Add the last digit // to the sum $sum = $sum + $n % 10; // Remove the last digit $n = $n / 10; } // Return the sum of the digits return $sum; } // Function to return the count of numbers // from the range [l, r] whose digit sum // is equal to y function countTheNumbers($l, $r, $y) { // Initialise the count to zero $count = 0; // Loop for each number // from the range for ($i = $l; $i <= $r; $i++) { // If digit sum = y if (sumOfDigits($i) == $y) $count++; } // Return the required count return $count; } // Driver code $l = 0; $r = 10; $y = 1; echo(countTheNumbers($l, $r, $y)); // This code is contributed // by Code_Mech. ?> |
2
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 Numbers in Range where the number does not contain more than K non zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- 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
- Count Odd and Even numbers in a range from L to R
- Count of numbers having only 1 set bit in the range [0, n]
- Count factorial numbers in a given range
- 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 different numbers that can be generated such that there digits sum is equal to 'n'
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.



