-2

here is the link to the file(google docs link) containing my code -

#include<stdio.h>

int len(int);
void main()
{
    int n,p;
    printf("enter number");
    scanf("%d",&n);
    p=len(n);
    printf("length of entered number is %d",p);
}
int len(int num)
{
    int i,c,b;
    for(i=1;i<=50;i++){
        b=10*i;
        if(num<b) {
            return(i);
            break;
        }
    }
}
001
  • 13,291
  • 5
  • 35
  • 66
  • Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Apr 19 '17 at 12:15
  • I also suggest you listen to your compiler and the warnings it gives you. And if it doesn't give you warnings then you need to enable more warnings. There are a few problems with the code you show, all detecable by the compiler. – Some programmer dude Apr 19 '17 at 12:19
  • May be you are trying to do `b=pow(10, i)`. But this is not a good idea. See the answers for better implementation – kuro Apr 19 '17 at 12:21
  • Notice that "number of digits in integer" and "length of entered number is" is potentially different when considering zero and negative numbers. – chux - Reinstate Monica Apr 19 '17 at 15:02
  • 1
    That's some food for thought.I will keep that in mind. @chux –  Apr 28 '17 at 12:54

3 Answers3

3

Computing b=10*i; makes no logical sense. Did you want to raise 10 to a given power?

The normal way of solving this problem is to repeatedly divide by 10 until 0 is reached (using integer arithmetic), and count the number of divisions made. That is the number of digits in the original number. This method is also not vulnerable to integer overflow:

unsigned digits = 0;
for (; num /= 10; ++digits);
return digits;
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • actualy correct method was to calculate pow(10,i) instead of 10*i. now the code is working fine. –  Apr 19 '17 at 12:41
  • 1
    Don't use pow for this: i) it's expensive, ii) you risk overflowing an `int`, iii) can go off due to floating point. `pow(x, y)` is typically implemented as `exp (y log x)` in C. Repeated division by 10 is better in every respect. – Bathsheba Apr 19 '17 at 13:29
  • `for (; num /= 10; ++digits);` fails for `num==0` as zero is usually considered a one digit number.. Consider `do { ++digits; } while (num /= 10);` – chux - Reinstate Monica Apr 19 '17 at 14:52
  • @chux: I probably should have mentioned zero as a special case. To me, defining the number of digits necessary to hold 0 is down to personal choice (I like to think of 0 as requiring 0 digits to store, but then I guess mathematicians are strange folk!) – Bathsheba Apr 19 '17 at 14:53
1
int len(int num){
    int i = 0;

    if(num == 0)
        return 1;

    while(num!=0){
        num = num / 10;
        i++;
    }
    return i;
}

here we divide num by 10 until it reaches 0, and each time loop executes we increment the count i++

sstefan
  • 385
  • 4
  • 15
-1

frontmatter i am not able to recognize your answer but i think this following code is helpful as per your title.

#include<stdio.h>
#include<conio.h>
void main()
{
    char a[50000];
    int i,c=0;
    printf("enter number:");
    fflush(stdin);
    gets(a);
    for(i=0;a[i]!='\0';i++)
    {
        if(a[i]>=0 || a[i]<=9)
        {
            c++;
        }
    }
    printf("\n %d digit number",c);
    getch();
}                        
Team Work
  • 35
  • 1
  • 7
  • 1
    `fflush(stdin);` = one cat potentially eaten due to undefined behaviour. See http://stackoverflow.com/questions/2979209/using-fflushstdin – Bathsheba Apr 19 '17 at 12:32
  • thanks for the help.but my code is now working when i replaced 10*i with pow(10,i) –  Apr 19 '17 at 12:51