Thread: Count Number of Digits in and Integer

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    21

    Count Number of Digits in and Integer

    Does anyone know if it's possible to count the number of digits in an integer? One of my friends recommended log10 or something but I can't figure it out.

    Redneon

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the number of digits in a base 10 number is equal to the number of times you multiply 1 times 10 to get to the same length (plus one to account for the first digit). So essentially, log 10 is that number. your buddy is correct.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Here you go should work as you need
    Code:
      
    int number,  temp, print;
    cin>>number;
    
    print = 0;
    while (temp != 0) {
    
     temp = number % 10;
     number = number / 10;
     print++;
    }
    print = print -1; // reason for this below
    cout<<print;
    i forget why but for some reason when usign a while loop. the loop will execute one time after the specified stopping point
    IE it would count one more then needed.
    try it without this and you'll see what i mean
    Last edited by Iamien; 08-18-2003 at 04:26 PM.

Popular pages Recent additions subscribe to a feed