You can use the same formula to grab the length of an integer through division, here is an example:
int intlen(int integer) {
int i = 1;
while ( (integer /= 10) > 0)
i++;
return i;
}If you happened to go over the limit of the formula (such as integer /= 100000000) it will return a negative result, so we can safely break the loop and return the true count of numbers within an integer.
You can then of course store the individual lengths in an array instead of a1, a2, a3 .... a6 to make the program more flexible.
//get length of sum
length = intlen(suma);
//prepare array to store results, a dynamic array of 'length' size
int results[length];
int divisor = 1;
//store results in array
for(i = 0; i <= length - 1; i++) {
results[i] = suma / divisor % 10;
divisor *= 10;
}notice how I set the divisor out of the loop, and increase it by 10 each time the loop happens so 10 -> 100 -> 1000 -> 10000 just like in your original equation, we then add it to the array dynamically using
i as an index.
You can now iterate through the loop, here we will just print it, but you can check for 3's and create a count on each occurrence without much trouble:
//now loop through array and print
for(i = 0; i <= length - 1; i++) {
printf("index %d = %d\n", i, results[i]);
}That code will work for any length number and result, as long as it is below integer max (~2.7 billion on 32 bit platforms, so you may wish to add checks for that if you wish)
A few more things you may wish to notice, such as array indexes starting at zero, so to iterate I use i = 0, and then length - 1 so I do not overrun the array and get a gibberish result from random memory, loops are quite easy once you get use to them!