-
-
Save pdparker/977ea977a1c8a189ab5b to your computer and use it in GitHub Desktop.
C script to split numbers into digits
This file contains hidden or 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
| /* compile using gcc digits.c -lm -o digits */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <math.h> | |
| #include <errno.h> | |
| #define DIGITS 5 | |
| void die(const char *message){ | |
| if(errno){ | |
| perror(message); | |
| } else{ | |
| printf("ERROR: %s\n", message); | |
| } | |
| exit(1); | |
| } | |
| int main(int argc, char *argv[]){ | |
| int i = 0; | |
| int tmp; | |
| int tmp2; | |
| for(i = 1; i < argc; i++){ | |
| tmp = atoi(argv[i]); | |
| int test = floor(log10 (abs (tmp))) + 1; | |
| if(test > DIGITS) die("Number is too large"); | |
| int j = 0; | |
| printf("Number: %d\n", tmp); | |
| for(j = 0; j < DIGITS; j++){ | |
| tmp2 = pow(10,j); | |
| printf("Number of %d's = %d\n", tmp2, tmp/tmp2 % 10); | |
| } | |
| printf("\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment