n-th number whose sum of digits is ten

Given an integer value n, find out the n-th positive integer whose sum is 10.

Examples:

Input : n = 2
Output : 27
The first number with sum of digits as
10 is 19. Second number is 27.

Input : 15
Output : 154



Method 1 (Simple):
We traverse through all numbers. For every number, we find sum of digits. We stop when we find n-th number with sum of digits as 10.

CPP

// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
  
int findNth(int n)
{
    int count = 0;
  
    for (int curr = 1; ; curr++) {
  
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
  
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
  
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
  
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}

Java

// Java program to find n-th number
// with sum of digits as 10.
  
import java.util.*;
import java.lang.*;
  
public class GFG
{
    public static int findNth(int n)
    {
        int count = 0;
        for (int curr = 1; ; curr++)
        {
  
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
  
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
  
    public static void main(String[] args)
    {
        System.out.print (findNth(5));
    }
}
  
// Contributed by _omg

Output:

55

Method 2 (Efficient):
If we take a closer look, we can notice that all multiples of 9 are present in arithmetic progression 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109, ….
However, there are numbers in above series whose sum of digits is not 9, for example, 100. So instead of checking one by one, we start with 19 and increment by 9.

CPP

// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
  
int findNth(int n)
{
    int count = 0;
  
    for (int curr = 19; ;curr += 9) {
  
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
  
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
  
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
  
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}

Java

// Java program to find n-th number
// with sum of digits as 10.
  
import java.util.*;
import java.lang.*;
  
public class GFG
{
    public static int findNth(int n)
    {
        int count = 0;
  
        for (int curr = 19; ; curr += 9)
        {
  
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
  
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
  
    public static void main(String[] args)
    {
        System.out.print (findNth(5));
    }
}
  
// Contributed by _omg

Output:

55


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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Practice Tags :

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)






User Actions