additive and multiplicative persistence

I wrote this code, to get the additive and multiplicative persistence and root.

It doesn't work with all numbers.
What do I need to change? and how can I make the program keep going and running?

Last edited on
> additive and multiplicative persistence and root.
¿what are they?


> It doesn't work with all numbers.
¿for example?


> how can I make the program keep going and running?
¿ah?


Also, use code tags.
Please format your code.

I've created a short program to calculate Additive Persistence (I hope I still remembers it correctly), I think you could just do the same to Multiplication Persistence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>

int AdditivePersistence(int number)
{
    int sum = 0;
    int i;
    
    for (i = number; i > 0; i /= 10)
        sum += i % 10;
    
    return number == sum ? 0 : (1 + AdditivePersistence(sum));
}

int main(void)
{
    int number = 2718;
    printf("Additive Persistence of %d is %d.\n", number, AdditivePersistence(number));
    return 0;
}
Topic archived. No new replies allowed.