Smallest number whose product with N has sum of digits equal to that of N | Java program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
class GFG { static void find_num ( String n ) { int ans = 0 ; char [ ] digitsOfN = n . toCharArray ( ) ; int sumOfDigitsN = 0 ; for ( char c : digitsOfN ) { sumOfDigitsN += Integer . parseInt ( Character . toString ( c ) ) ; } for ( int x = 11 ; x > 0 ; x ++ ) { int newNum = x * Integer . parseInt ( n ) ; int tempSumDigits = 0 ; char [ ] temp = Integer . toString ( newNum ) . toCharArray ( ) ; for ( char c : temp ) { tempSumDigits += Integer . parseInt ( Character . toString ( c ) ) ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String N = "3029" ; find_num ( N ) ; } }