Smallest integer with digit sum M and multiple of N | C # implementation of the above approach ; Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code
using System ; class GFG { static int digitSum ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n % 10 ; n /= 10 ; } return ans ; } static int findInt ( int n , int m ) { int minDigit = ( int ) Math . Floor ( ( double ) ( m / 9 ) ) ; int start = ( int ) Math . Pow ( 10 , minDigit ) - ( int ) Math . Pow ( 10 , minDigit ) % n ; while ( start < int . MaxValue ) { if ( digitSum ( start ) == m ) return start ; else start += n ; } return - 1 ; } static public void Main ( ) { int n = 13 , m = 32 ; Console . WriteLine ( findInt ( n , m ) ) ; } }