Anonymous user
Middle three digits: Difference between revisions
→{{header|C}}: replaced incorrect implementation ("&128" was not reasonable; inherent overflow problem)
(→{{header|PARI/GP}}: Marked incorrect as valid answers must always have three digits) |
(→{{header|C}}: replaced incorrect implementation ("&128" was not reasonable; inherent overflow problem)) |
||
Line 73:
0: ****number of digits must be >= 3 and odd****</pre>
=={{header|C}}==#include <stdio.h>
#include <string.h>
// we return a static buffer; caller wants it, caller copies it
char * mid3(int n)
{
sprintf(buf, "%d", n > 0 ? n : -n);
l = strlen(buf);
l =
buf[l + 3] = 0;
return buf + l;
▲ if (mag & 128) return E_EVEN;
return 0;▼
}
int main(void)
{
int x[] = {123, 12345, 1234567, 987654321, 10001, -10001,
▲ int num;
-123, -100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0,
▲ char buf[4] = "";
1234567890};
printf("%d: ", num);▼
int i;
char *m;
for (i = 0; i < sizeof(x)/sizeof(x[0]); i++) {
if (!(m = mid3(x[i])))
m = "error";
}
}</lang>
=={{header|C++}}==
|