# n-th number whose sum of digits is ten in C++

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that finds the n-th numbers whose digits sum is ten.

Let's see the sequence of number whose digits sum is 10.

19, 28, 37, 46, 55, etc..,

Each number is incremented by 9 in the above sequence. There are numbers in the above sequence whose digits sum does not equal 10. So, we can have a loop that increments by 9 and checks for digits sum and finds the n-th number.

Let's see the steps to solve the problem.

• Initialise the number n
• Initialise a counter to 0.
• Write a loop that iterates from 19
• If the current number digits sum is 10, increment the counter by 1.
• If the counter is equal to n, then return the current number.
• Increment the iterative variable by 9.

## Example

Let's see the code.

Live Demo

#include <bits/stdc++.h>
using namespace std;
int findNthNumber(int n) {
int count = 0, i = 19;
while (true) {
int sum = 0;
for (int number = i; number > 0; number = number / 10) {
sum = sum + number % 10;
}
if (sum == 10) {
count++;
}
if (count == n) {
return i;
}
i += 9;
}
return -1;
}
int main() {
int n = 7;
cout << findNthNumber(7) << endl;
return 0;
}

## Output

If you run the above code, then you will get the following result.

73

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.