I wrote this simple program to add the digits of a number k. The code is this:

```
#include<stdio.h>
#include<math.h>
int digitsum(int k)
{
    // This line will find the number of digits in k.
    int size = floor(log(k)/log(10))+1;
    int sum = 0, i, j;
    // Here I find the sum of digits starting with the leading digit.
    for (i=1;i<=size;i++)
    {
        j = floor(k*pow(10,-size+i)); // Leading digit
        sum = sum + j; // Add to sum
        k = k - j*pow(10,size-i); // Delete first digit
        //printf("%d   %d \n", j, k);
    }
    return sum;
}
main()
{
    int k =1141;
    int sum = digitsum(k);
    printf("%d", sum);
}

```

So sometimes my program works, for instance for k=141 it returns 6 but for k=1141 it also returns 6... I realize there is an easier way to do this task by taking the mod 10 and then dividing the number by 10. However I think this code should work. Any ideas what is going wrong? Thanks in advance. Also, I am just starting working with C, if there are any things that are not proper coding or rookie mistakes feel free to let me know. Thanks a lot!