Last active
December 25, 2015 20:19
-
-
Save ButchDean/7034499 to your computer and use it in GitHub Desktop.
Non-iterative approach to finding the number of digits in an integer. Also works for both positive and negative numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#include <stdlib.h> | |
int main(int argc, char** argv) | |
{ | |
int digits; | |
digits = (atoi(argv[1]) == 0) ? 1 : (int)log10(abs(atoi(argv[1]))) + 1; | |
printf("Number of digits in %s is %d\n", argv[1], digits); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment