Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple C program to decompose a number in powers of 10
#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
You can’t perform that action at this time.