Home > Net >  product of digits not including 0 on C
product of digits not including 0 on C

Time:09-23

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c;
    b=1;
    a=0;
    scanf("%d", &a);
    while (a>0)
    {
        c=a;
        if (c==0) 
            b=b*1; 
        else {
            b=b*(a); 
            a=a/10;
        }
    }
    printf("Proizvedenie: %d\n", b);
    return 0;
}

It doesnt work when I add 0 in any position of number, but i added if and dont know why it doesnt work, cycle just does not end, pls help

01234 - 24; Right
1234 - 24; Right
12304 - doesnt work
12340 - doesnt work

WHY PLS HELP =(

CodePudding user response:

You're only dividing a by 10 when the current digit is not 0. When the digit is 0 a stays the same and you and up in an infinite loop.

Move the update of a to outside of the if block. Also, b=b*1 is a no-op, so you can remove that.

while (a>0)
{
    c=a;
    if (c!=0) {
        b=b*c; 
    }
    a=a/10;
}

CodePudding user response:

You're only doing a = a/10 in the else block. So when you get to a 0 digit, you stop dividing by 10 and get stuck in an infinite loop.

Take that line out of the if/then, since it needs to be done every time.

    while (a>0)
    {
        c=a;
        if (c!=0) {
            b *= c;
        }
        a /= 10;
    }

CodePudding user response:

unsigned int product(int x)
{
    int result = !!x;
    while(x)
    {   
        if(x % 10) result *= abs(x % 10);
        x /= 10;
    }
    return result;
}
int main(void)
{
    int testData[] = {0, 1234, 12304, 12340, -1234, -12304, -012340  /* <- octal number!! */};

    for(int x = 0; x < sizeof(testData)/sizeof(testData[0]); x   )
        printf("%d - %u\n", testData[x], product(testData[x]));
}

https://godbolt.org/z/T8T1a5YEE

  1. Use functions.
  2. integers can be negative as well
  3. You need to handle zero too.

In your code:

    c=a;
    if (c==0) 
        b=b*1; 
    else {
        b=b*(a); 
        a=a/10;
    }

you need to move a = a / 10 outside the if as it is only executed when a % 10 is not zero. If it is, a does not change and you are in an infinitive loop

CodePudding user response:

Others have noted the flaw in your code (location of a=a/10; inside else code block.

Here is a branchless way to achieve your objective. "Branching" (using an if() conditional) can make code run much slower when the processor's pipeline prediction turns out to be wrong. When processing millions (or billions) of data items, this may be a significant cost in time.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int a = 0, prod = 1;

    if( scanf( "%d", &a ) != 1 ) {
        fprintf( stderr, "scanf failed\n" );
        return 1;
    }

    for( a = abs(a); a; a /= 10 ) {
        int r = a;
        prod *= r (r==0); // branchless technique
    }

    printf( "Proizvedenie: %d\n", prod );

    return 0;
}
54300012
Proizvedenie: 120

When the value of r is one of 1-9, r==0 has the value 0.
When the value of r is 0 and r==0 has the value 1.
So the 10 different rhs values can be 1, 1, 2, 3, 4, 5, 6, 7, 8 or 9.

  •  Tags:  
  • c
  • Related