#include <stdio.h> #include <string.h> int sum_of_substrings(char *num) { int sum = 0; int len = strlen(num); for (int i = 0; i < len; i++) { int val = 0; for (int j = i; j < len; j++) { val = val * 10 + (num[j] - '0'); sum += val; } } return sum; } int main() { char num[] = "1234"; printf("Sum of all substrings of '%s': %d\n", num, sum_of_substrings(num)); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter