This is for a CS course I am taking. I'm stuck.
I need to write a program that takes a number and tallies up how many cycles it takes of multiplying the digits until it is a single digit number. ie:
715 = 7 * 1 * 5 =
35 = 3 * 5 =
15 = 1 * 5 =
5
Persistence is 3.
27 = 2 * 7 =
14 = 1 * 4 =
4
Persistence is 2.
Etc.
What I have written works for some numbers, but not all. For example, double digit numbers such as 44 and 66 return the wrong persistence, but others like 19, 40, etc. return the correct one. Can someone help me out?
#include <stdio.h>
int persistence( int input );
int main( int argc, char *argv[] ) {
int input, persist = 0, number, product = 1;
printf( "\n - Persistence Check -\n\n" );
while ( input != EOF ) {
printf( "\n" );
printf( "Enter an integer: " );
scanf( "%d", &input );
persist = persistence( input );
printf( "\n\nThe persistence of %d is %d.\n\n", input, persist );
}
return 0;
}
int persistence( int input ) {
int digit, x = 1, persist = 0;
while ( input > 9 ) {
do {
digit = input % 10;
x = x * digit;
input = input / 10;
} while ( input > 0 );
persist++;
input = x;
}
return persist;
}