Skip to content

Instantly share code, notes, and snippets.

@pdparker
Created July 23, 2015 00:10
Show Gist options
  • Select an option

  • Save pdparker/977ea977a1c8a189ab5b to your computer and use it in GitHub Desktop.

Select an option

Save pdparker/977ea977a1c8a189ab5b to your computer and use it in GitHub Desktop.
C script to split numbers into digits
/* 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