Sum of all substrings of a string representing a number | Set 1 | C # program to print sum of all substring of a number represented as a string ; Utility method to convert character digit to integer digit ; Returns sum of all subString of num ; Storing prev value ; SubStrings sum upto current index loop over all digits of String ; Update each sumofdigit from previous value ; Add current value to the result ; Update previous ; Driver code to test above methods
using System ; class GFG { static int toDigit ( char ch ) { return ( ch - '0' ) ; } static int sumOfSubStrings ( string num ) { int n = num . Length ; int prev = toDigit ( num [ 0 ] ) ; int res = prev ; int current = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; current = ( i + 1 ) * numi + 10 * prev ; res += current ; prev = current ; } return res ; } static void Main ( ) { string num = "1234" ; Console . WriteLine ( sumOfSubStrings ( num ) ) ; } }