Find the smallest number whose sum of digits is N Last Updated : 22 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given a positive integers N, the task is to find the smallest number whose sum of digits is N.Example: Input: N = 10Output: 19Explanation: 1 + 9 = 10 = N Input: N = 18Output: 99Explanation: 9 + 9 = 18 = N Naive Approach: A Naive approach is to run a loop of i starting from 0 and find Sum of digits of i and check if it's equal to N or not. Below is the implementation of the above approach. C++ // C++ program to find the smallest // number whose sum of digits is also N #include <iostream> #include <math.h> using namespace std; // Function to get sum of digits int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Function to find the smallest // number whose sum of digits is also N void smallestNumber(int N) { int i = 1; while (1) { // Checking if number has // sum of digits = N if (getSum(i) == N) { cout << i; break; } i++; } } // Driver code int main() { int N = 10; smallestNumber(N); return 0; } Java // Java program to find the smallest // number whose sum of digits is also N class GFG{ // Function to get sum of digits static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Function to find the smallest // number whose sum of digits is also N static void smallestNumber(int N) { int i = 1; while (1 != 0) { // Checking if number has // sum of digits = N if (getSum(i) == N) { System.out.print(i); break; } i++; } } // Driver code public static void main(String[] args) { int N = 10; smallestNumber(N); } } // This code is contributed // by shivanisinghss2110 Python3 # Python3 program to find the smallest # number whose sum of digits is also N # Function to get sum of digits def getSum(n): sum1 = 0; while (n != 0): sum1 = sum1 + n % 10; n = n // 10; return sum1; # Function to find the smallest # number whose sum of digits is also N def smallestNumber(N): i = 1; while (1): # Checking if number has # sum of digits = N if (getSum(i) == N): print(i); break; i += 1; # Driver code N = 10; smallestNumber(N); # This code is contributed by Code_Mech C# // C# program to find the smallest // number whose sum of digits is also N using System; class GFG{ // Function to get sum of digits static int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Function to find the smallest // number whose sum of digits is also N static void smallestNumber(int N) { int i = 1; while (1 != 0) { // Checking if number has // sum of digits = N if (getSum(i) == N) { Console.Write(i); break; } i++; } } // Driver code public static void Main(String[] args) { int N = 10; smallestNumber(N); } } // This code is contributed by Amit Katiyar JavaScript <script> //Javascript program to find /the smallest // number whose sum of digits is also N // Function to get sum of digits function getSum(n) { let sum = 0; while (n != 0) { sum = sum + n % 10; n = Math.floor(n / 10); } return sum; } // Function to find the smallest // number whose sum of digits is also N function smallestNumber(N) { let i = 1; while (1) { // Checking if number has // sum of digits = N if (getSum(i) == N) { document.write(i); break; } i++; } } // Driver code let N = 10; smallestNumber(N); // This code is contributed by Mayank Tyagi </script> Output19 Time Complexity: O(N).Auxiliary space: O(1) Efficient Approach: An efficient approach to this problem is an observation. Let's see some examples. If N = 10, then ans = 19If N = 20, then ans = 299If N = 30, then ans = 3999So, it is clear that the answer will have all digits as 9 except the first one so that we get the smallest number.So, the Nth term will be = (N \bmod 9 + 1) * 10^\frac{N}{9} - 1 Below is the implementation of the above approach C++ // C++ program to find the smallest // number whose sum of digits is also N #include <iostream> #include <math.h> using namespace std; // Function to find the smallest // number whose sum of digits is also N void smallestNumber(int N) { cout << (N % 9 + 1) * pow(10, (N / 9)) - 1; } // Driver code int main() { int N = 10; smallestNumber(N); return 0; } Java // Java program to find the smallest // number whose sum of digits is also N class GFG{ // Function to find the smallest // number whose sum of digits is also N static void smallestNumber(int N) { System.out.print((N % 9 + 1) * Math.pow(10, (N / 9)) - 1); } // Driver code public static void main(String[] args) { int N = 10; smallestNumber(N); } } // This code is contributed by sapnasingh4991 Python3 # Python3 program to find the smallest # number whose sum of digits is also N # Function to find the smallest # number whose sum of digits is also N def smallestNumber(N): print((N % 9 + 1) * pow(10, (N // 9)) - 1) # Driver code N = 10 smallestNumber(N) # This code is contributed by Code_Mech C# // C# program to find the smallest // number whose sum of digits is also N using System; class GFG{ // Function to find the smallest // number whose sum of digits is also N static void smallestNumber(int N) { Console.WriteLine((N % 9 + 1) * Math.Pow(10, (N / 9)) - 1); } // Driver code public static void Main() { int N = 10; smallestNumber(N); } } // This code is contributed by Ritik Bansal JavaScript <script> // Javascript program to find the smallest // number whose sum of digits is also N // Function to find the smallest // number whose sum of digits is also N function smallestNumber(N) { document.write( (N % 9 + 1) * Math.pow(10, parseInt(N / 9, 10)) - 1 ); } let N = 10; smallestNumber(N); </script> Output19 Time complexity: O(logN) since inbuilt pow function is being usedAuxiliary space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Find the smallest number whose sum of digits is N spp____ Follow Improve Article Tags : Mathematical Computer Science Fundamentals DSA number-digits Practice Tags : Mathematical Similar Reads Find the kth smallest number with sum of digits as m Given two integers M and K, the task is to find the Kth smallest number with digit sum as M.Examples: Input: M = 5, K = 3 Output: 23 Sequence of numbers starting from 1 with digit sum as 5 is as follows: 5 14 23 32 41 So 3rd smallest number is 23Input: M = 4, K = 1 Output: 4 Approach: We need to fin 6 min read Smallest number whose sum of digits is square of N Given an integer N, the task is to find the smallest number whose sum of digits is N2. Examples: Input: N = 4 Output: 79 24 = 16 sum of digits of 79 = 76 Input: N = 6 Output: 9999 210 = 1024 which has 4 digits Approach: The idea is to find the general term for the smallest number whose sum of digits 3 min read 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: 28 The first number with sum of digits as 10 is 19. Second number is 28. Input: 15 Output: 154 Method 1 (Simple): We traverse through all numbers. For every number, we find the sum of digits. 8 min read Find smallest number with given digits and sum of digits Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 9 min read Smallest odd number with even sum of digits from the given number N Given a large number in form of string str. The task is to find the smallest odd number whose sum of digits is even by removing zero or more characters from the given string str, where the digits can be rearranged. Examples Input: str = "15470" Output: 15 Explanation: Two smallest odd digits are 1 6 min read What is the smallest 4 digit number? The method to represent and work with numbers is known as the number system. A number system is a system of writing to represent numbers. It is the mathematical notation used to represent numbers of a given set by using digits or other symbols. It has arithmetic operations to perform division, multi 5 min read Find the smallest number whose digits multiply to a given number n Given a number 'n', find the smallest number 'p' such that if we multiply all digits of 'p', we get 'n'. The result 'p' should have minimum two digits.Examples: Input: n = 36 Output: p = 49 // Note that 4*9 = 36 and 49 is the smallest such number Input: n = 100 Output: p = 455 // Note that 4*5*5 = 1 8 min read Smallest number whose product with N has sum of digits equal to that of N Given an integer N, the task is to find the smallest positive integer, which when multiplied by N, has sum of digits equal to the sum of digits of N. Examples: Input: N = 4Output: 28Explanation: Sum of digits of N = 44 * 28 = 112Sum of digits = 1 + 1 + 2 = 4, which is equal to sum of digits of N. In 6 min read Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of 6 min read Smallest N digit number whose sum of square of digits is a Perfect Square Given an integer N, find the smallest N digit number such that the sum of the square of digits (in decimal representation) of the number is also a perfect square. If no such number exists, print -1.Examples: Input : N = 2 Output : 34 Explanation: The smallest possible 2 digit number whose sum of squ 15+ min read Like