0

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;
}
3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by linux
0

You're not properly resetting between persistence iterations. For example, x doesn't get reset to 1.

0

x is always storing some value,for the next do while loop
so better you do:-

int persistence( int input ) {

    int digit, x , persist = 0;

    while ( input > 9 ) {
     [B]x=1;[/B]
        do {
            digit = input % 10;
            x = x * digit;
            input = input / 10;
        } while ( input > 0 );

        persist++;
        input = x;
    }

    return persist;
}
0

Ahhh, you're the best! For anyone who cares or gets assigned something like this in the future, here's the working source.

#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 ) {
        x = 1;
        do {
            digit = input % 10;
            x = x * digit;
            input = input / 10;

        } while ( input > 0 );

        persist++;
        input = x;
    }

    return persist;
}

Thank you so much!!!

Edited by linux: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.