rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1
for ( i = 1 ; i <= d - rem ; i ++ ) { num = num + to_string ( val ) ; }