Sum of the digits of n is substring of n: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add PL/M |
Not a robot (talk | contribs) Add C |
||
Line 60: | Line 60: | ||
903 913 923 933 943 |
903 913 923 933 943 |
||
953 963 973 983 993</pre> |
953 963 973 983 993</pre> |
||
=={{header|C}}== |
|||
<lang c>#include <stdio.h> |
|||
#include <string.h> |
|||
int digitSum(int n) { |
|||
int s = 0; |
|||
do {s += n % 10;} while (n /= 10); |
|||
return s; |
|||
} |
|||
int digitSumIsSubstring(int n) { |
|||
char s_n[32], s_ds[32]; |
|||
sprintf(s_n, "%d", n); |
|||
sprintf(s_ds, "%d", digitSum(n)); |
|||
return strstr(s_n, s_ds) != NULL; |
|||
} |
|||
int main() { |
|||
int i; |
|||
for (i=0; i<1000; i++) |
|||
if (digitSumIsSubstring(i)) |
|||
printf("%d ",i); |
|||
printf("\n"); |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 109 119 129 139 149 159 169 179 189 199 200 300 400 500 600 700 800 900 910 911 912 913 914 915 916 917 918 919</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |