- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count of numbers from range[L, R] whose sum of digits is Y in C++

We are provided two numbers START and END to define a range of numbers.The goal is to find all the numbers in the range [START,END] which have sum of digits equal to a given number Y.

We will do this by traversing numbers from START to END and for each number we will count the sum of its digit using a while loop. If this sum is equal to Y, increment count.

Let’s understand with examples.

**Input**

START=10 END=20 Y=4

**Output**

Numbers such that digit sum is equal to Y: 1

**Explanation**

Number 13 has digit sum equal to 4.

**Input**

START=10 END=50 Y=5

**Output**

Numbers such that digit sum is equal to Y: 5

**Explanation**

Numbers 14, 23, 32, 41 and 50 have digit sum 5.

## Approach used in the below program is as follows

We take integers START, END, Y.

Function digitSum(int start, int end, int y) returns the count of numbers with digitsum=y

Take the initial variable count as 0 for such numbers.

Take variable digsum as 0

Traverse range of numbers using for loop. i=start to i=end

Now for each number num=i, using while loop check if number is >0.

calculate digsum+=num%10. Reduce num=num/10 to add the next digit.

At the end of the while, check if ( digsum == d ). If true increment count.

At the end of all loops count will have a total number which satisfies the condition.

Return the count as result.

## Example

#include <bits/stdc++.h> using namespace std; int digitSum(int start, int end, int y){ int count = 0; int digsum = 0; for (int i = start; i <= end; i++){ int num=i; digsum=0; while(num>0){ digsum+=num%10; //sum of digits num=num/10; } if(digsum==y) //original number is i{ count++; //cout<<i<<" "; } } return count; } int main(){ int START = 100; int END = 1000; int Y = 5; cout <<"Numbers such that digit sum is equal to Y: "<<digitSum(START,END,Y); return 0; }

## Output

If we run the above code it will generate the following output −

Numbers such that digit sum is equal to Y: 15

- Related Questions & Answers
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Find numbers whose sum of digits equals a value
- Count the nodes in the given tree whose sum of digits of weight is odd in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- n-th number whose sum of digits is ten in C++
- Count of Range Sum in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- Compute sum of digits in all numbers from 1 to n
- Minimum sum of two numbers formed from digits of an array in C++
- Program to count number of paths whose sum is k in python
- Find M-th number whose repeated sum of digits of a number is N in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript