A simple C program to decompose a number in powers of 10
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 <stdlib.h> | |
#include <math.h> | |
#include <inttypes.h> | |
#define POWER(x, y) (long int)pow(x, (float)y) | |
uint8_t* puzzledNumber(int32_t number, uint16_t *size) | |
{ | |
int16_t i = 0, j = 0, k = 0; | |
while(POWER(10, *size) <= number) | |
{ | |
(*size)++; | |
} | |
uint8_t *tab = calloc(sizeof(*tab), *size); | |
for(i = 0; i < *size; i++) | |
{ | |
tab[i] = number / POWER(10, *size - i - 1); | |
for(j = i - 1, k = 1; j >= 0; j--, k++) | |
{ | |
tab[i] -= tab[j] * POWER(10, k); | |
} | |
} | |
return tab; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
(void)argc; | |
(void)argv; | |
int32_t number = 0; | |
scanf("%d", &number); | |
uint16_t size = 0; | |
uint8_t *tab = puzzledNumber(number, &size); | |
if(tab != NULL) | |
{ | |
for(uint16_t i = 0; i < size; i++) | |
{ | |
printf("%d ", tab[i]); | |
} | |
printf("\n"); | |
free(tab); | |
tab = NULL; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment