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 = 2Input: 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
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.

