Given an integer **N**, the task is to find the smallest number whose sum of digits is N^{2}.**Examples:**

Input:N = 4Output:79

2^{4}= 16

sum of digits of 79 = 76Input:N = 6Output:9999

2^{10}= 1024 which has 4 digits

**Approach**: The idea is to find the general term for the smallest number whose sum of digits is square of N. That is

// First Few terms First Term = 1 // N = 1 Second Term = 4 // N = 2 Third Term = 9 // N = 3 Fourth Term = 79 // N = 4 . . N^{th Term: }

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to return smallest` `// number whose sum of digits is n^2` `int` `smallestNum(` `int` `n)` `{` ` ` `return` `(n * n % 9 + 1)` ` ` `* ` `pow` `(10, n * n / 9)` ` ` `- 1;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 4;` ` ` `cout << smallestNum(n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to return smallest` `// number whose sum of digits is n^2` `static` `int` `smallestNum(` `int` `n)` `{` ` ` `return` `(` `int` `)((n * n % ` `9` `+ ` `1` `) * ` ` ` `Math.pow(` `10` `, n * n / ` `9` `) - ` `1` `);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `n = ` `4` `;` ` ` ` ` `System.out.print(smallestNum(n));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python 3

`# Python implementation of the above approach ` `# Function to return smallest ` `# number whose sum of digits is n^2 ` `def` `smallestNum(n):` ` ` `return` `((n ` `*` `n ` `%` `9` `+` `1` `) ` `*` ` ` `pow` `(` `10` `, ` `int` `(n ` `*` `n ` `/` `9` `)) ` `-` `1` `) ` `# Driver Code` `# Given N` `N ` `=` `4` `print` `(smallestNum(N))` `# This code is contributed by Vishal Maurya.` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach` `using` `System;` `class` `GFG{` `// Function to return smallest` `// number whose sum of digits is n^2` `static` `int` `smallestNum(` `int` `n)` `{` ` ` `return` `(` `int` `)((n * n % 9 + 1) * ` ` ` `Math.Pow(10, n * n / 9) - 1);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `n = 4;` ` ` ` ` `Console.Write(smallestNum(n));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

79

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest number with given sum of digits and sum of square of digits
- Smallest number whose square has N digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find smallest 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
- Find smallest perfect square number A such that N + A is also a perfect square number
- Sum of the digits of square of the given number which has only 1's as its digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Find the smallest number whose sum of digits is N
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the smallest number whose digits multiply to a given number n
- Check if a number is a perfect square having all its digits as a perfect square
- Find M-th number whose repeated sum of digits of a number is N
- Smallest odd number with even sum of digits from the given number N
- Find the Largest number with given number of digits and sum of digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum

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.