Given two positive integers N and M. The task is to find M-th number whose sum of digits of a number until sum becomes single digit is N.

**Examples:**

Input:N = 1, M = 3Output:19 The first two numbers being 1 and 9.Input:N = 2, M = 5Output:38 The first four numbers being 2, 11, 20 and 29.

A **naive** approach is to iterate for all numbers and keep a count of number whose sum returns N.

An **efficient** approach is to find the summation of digits till it becomes single digits in O(1) that has been discussed here. Hence the formula to find M-th number will be:

M

^{th}number: (M-1)*9 + N

Below is the implementation of the above approach:

## C++

`// C++ program to Find m-th number whose ` `// sum of digits of a number until ` `// sum becomes single digit is N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the M-th ` `// number whosesum till one digit is N ` `int` `findNumber(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `num = (m - 1) * 9 + n; ` ` ` `return` `num; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 2, m = 5; ` ` ` `cout << findNumber(n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Find m-th number whose ` `// sum of digits of a number until ` `// sum becomes single digit is N ` `class` `GFG ` `{ ` ` ` `// Function to find the M-th ` `// number whosesum till one digit is N ` `static` `int` `findNumber(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `num = (m - ` `1` `) * ` `9` `+ n; ` ` ` `return` `num; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `2` `, m = ` `5` `; ` ` ` `System.out.print(findNumber(n, m)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Find m-th number ` `# whose sum of digits of a number ` `# until sum becomes single digit is N ` ` ` `# Function to find the M-th ` `# number whosesum till one digit is N ` `def` `findNumber(n, m) : ` ` ` ` ` `num ` `=` `(m ` `-` `1` `) ` `*` `9` `+` `n; ` ` ` `return` `num; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `2` `; ` ` ` `m ` `=` `5` `; ` ` ` `print` `(findNumber(n, m)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Find m-th number whose ` `// sum of digits of a number until ` `// sum becomes single digit is N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the M-th ` `// number whosesum till one digit is N ` `static` `int` `findNumber(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `num = (m - 1) * 9 + n; ` ` ` `return` `num; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 2, m = 5; ` ` ` `Console.Write(findNumber(n, m)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Find m-th number whose ` `// sum of digits of a number until ` `// sum becomes single digit is N ` ` ` `// number whosesum till one digit is N ` `function` `findNumber(` `$n` `, ` `$m` `) ` `{ ` ` ` `$num` `= (` `$m` `- 1) * 9 + ` `$n` `; ` ` ` `return` `$num` `; ` `} ` ` ` `// Driver Code ` `$n` `= 2; ` `$m` `= 5; ` `echo` `findNumber(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

38

## Recommended Posts:

- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Sum of P terms of an AP if Mth and Nth terms are given
- Find Pth term of a GP if Mth and Nth terms are given
- Find the Mth element of the Array after K left rotations
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Recursive sum of digits of a number formed by repeated appends
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Ratio of mth and nth terms of an A. P. with given ratio of sums
- Mth element after K Right Rotations of an Array
- Mth bit in Nth binary string from a sequence generated by the given operations
- Count ways to spell a number with repeated digits
- Remove repeated digits in a given number
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Total numbers with no repeated digits in a range
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Find a Number X whose sum with its digits is equal to N
- Find the smallest number whose sum of digits is 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.