You're viewing a single thread.
All Comments
183 comments
jsdz @lemmy.ml int is_even(int n) { int result = -1; char number[8]; //should be enough sprintf(number, "%d", n); // check the number // TODO: handle negative numbers for (char *p=number; *p; p++) { if (*p=='0' || *p=='2' || *p=='4' || *p=='6' || *p=='8') result = 1; else if (*p=='1' || *p=='3' || *p=='5' || *p=='7' || *p=='9') result = 0; else { fprintf(stderr, "Your number is wrong!\n"); exit(1); } } return result; }
13 0 ReplyAnxiousDuck @feddit.it This should be the accepted answer
3 0 ReplyEvokerKing @lemmy.world Why not one with modulus?
3 0 ReplyMolochAlter @lemmy.world This one is more readable.
2 2 Reply
183 comments
Scroll to top