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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, 1
print(countTheNumbers(l, r, y))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.