Home > Mobile >  This code is running properly when I use 1 digit numbers. But It's keep lagging on more digit n
This code is running properly when I use 1 digit numbers. But It's keep lagging on more digit n

Time:01-20

Something is wrong. I'm trying to make a cade which can count number count of any natural number. Like number count of 2 is 1, 30 is 2, 456 is 3. My code is running for 1 digit numbers but not for two digit numbers.

#include<stdio.h>
void main(void)
{
    int num,count,check;
    float div;
    printf("Enter a natural number\n");
    scanf("%d", &num);

    while (num<=0)
    {
        printf("Error\n");
        printf("Enter a number\n");
        scanf("%d", &num);        
    }

    while(num>1)
    { 
        count=1;
        check=10;
        div=num/check;

        if(div<=1)
        {
            printf("Number count is\n%d", count);
            break;
        }
        check = check*10;
        count = count 1;
    }
}

CodePudding user response:

It is time to learn to use a debugger. Using it would have immediately shown the major problem in your code: you reset the value of count and check inside the loop. So if you enter a number greater or equal to 10, you enter an infinite loop because you will consistently divide that number by 10 and find that the result is >= 1!

There is another less important problem: you use if(div<=1) when it should be if(div<1). Because 10/10 is 1 and has 2 digits...

After those fixes you should have:

    ...
    check = 10;
    count = 1;
    while (num > 1)
    {
        div = num / check;

        if (div < 1)
        {
            printf("Number count is\n%d", count);
            break;
        }
        check = check * 10;
        count = count   1;
    }
    return 0; // main shall return an int value to its environment...
}

Which correctly gives the number of decimal digit on positive integers. But as you were said in comments, you should always test the return value of scanf (what is the user inadvertently types a t instead of 5 for example?).

That being said, this answer intends to show you what the problems were, but Keyne's solution is better...

CodePudding user response:

The problem with your solution is that after check and count are modified at the end of the loop, they are re-declared to 1 and 10 respectively at the beginning of the loop at every passage.
You need to move the declaration just before the while loop.
Also div doesn't need to be a float given that the decimal part of this number is irrelevant.
You could also use less variables by replacing check by 10 and using num directly instead of temporarily storing results in div.

I think this might be a simpler solution

#include <stdio.h>

int main(){
    int num = 0, digits = 0;

    while (num <= 0)
    {
        printf("Enter a natural number\n");
        scanf("%d", &num);
        num == 0 ? printf("Error\n") : 0;
    }

    for( ; num > 0; digits  )
        num /= 10;
    printf("number of digits: %d\n", digits);
}

As num is continuously divided by 10, the decimal of the result gets truncated since num is an int while digits steadily increases.

  •  Tags:  
  • Related