Print a number containing K digits with digital root D
Given a digital root ‘D’ and number of digits ‘K’. The task is to print a number containing K digits that has its digital root equal to D. Print ‘-1’ if such a number does not exist.
Examples:
Input: D = 4, K = 4 Output: 4000 No. of digits is 4. Sum of digits is also 4. Input: D = 0, K = 1 Output: 0
Approach: A key observation to solving this problem is that appending any number of 0s to a number does not change its digital root. Hence D followed by (K-1) 0’s is a simple solution.
Special case when D is 0 and K is not 1 does not have a solution since the only number with digital root 0 is 0 itself.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to find a number void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) cout << "-1"; else { cout << d; k--; // Print k-1 zeroes while (k--) cout << "0"; } } // Driver code int main() { int k = 4, d = 4; printNumberWithDR(k, d); return 0; } |
Java
// Java implementation of the above approach import java.io.*; class GFG { // Function to find a number static void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) System.out.print( "-1"); else { System.out.print(d); k--; // Print k-1 zeroes while (k-->0) System.out.print( "0"); } } // Driver code public static void main (String[] args) { int k = 4, d = 4; printNumberWithDR(k, d); } } //This code is contributed by inder_verma.. |
Python3
# Python3 implementation of # the above approach # Function to find a number def printNumberWithDR( k, d ) : # If d is 0, k has to be 1 if d == 0 and k != 1 : print(-1, end = "") else : print(d, end = "") k -= 1 # Print k-1 zeroes while k : print(0,end = "") k -= 1 # Driver code if __name__ == "__main__" : k, d = 4, 4 # Function call printNumberWithDR( k, d ) # This code is contributed by # ANKITRAI1 |
C#
// C# implementation of the above approach using System; class GFG { // Function to find a number static void printNumberWithDR(int k, int d) { // If d is 0 k has to be 1 if (d == 0 && k != 1) Console.Write( "-1"); else { Console.Write(d); k--; // Print k-1 zeroes while (k-->0) Console.Write( "0"); } } // Driver code static public void Main () { int k = 4, d = 4; printNumberWithDR(k, d); } } // This code is contributed by ajit. |
PHP
<?php // PHP implementation of the above approach // Function to find a number function printNumberWithDR($k, $d) { // If d is 0 k has to be 1 if ($d == 0 && $k != 1) echo "-1"; else { echo $d; $k--; // Print k-1 zeroes while ($k--) echo "0"; } } // Driver code $k = 4; $d = 4; printNumberWithDR($k, $d); // This code is contributed // by akt_mit ?> |
4000
Time complexity: O(K)
Recommended Posts:
- Find Nth positive number whose digital root is X
- Digital Root (repeated digital sum) of the given large integer
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Numbers in a Range with given Digital Root
- Sudo Placement[1.7] | Greatest Digital Root
- Print a number strictly less than a given number such that all its digits are distinct.
- Print path from root to all nodes in a Complete Binary Tree
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- 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
- Minimum number of digits to be removed so that no two consecutive digits are same
- Find the average of k digits from the beginning and l digits from the end of the given number
- Smallest number with given sum of digits and sum of square of digits
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.



