logo elektroda
logo elektroda
X
logo elektroda

[C++] Counting Digits in an Input Number: How to Calculate & Display Total Digits

Mirdon 13973 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 741303
    Mirdon
    Level 10  
    How to count the number of digits that make up the number I enter into the program, I mean that the program asks for a number, I give, for example, 151 and the program calculates that the number consists of 3 digits, how to count?

    Of course in c++

    closing. - arnoldziq
  • ADVERTISEMENT
  • #2 741428
    h-doc
    Level 27  
    simple - you divide by 10 and check if the number is greater than 1 - if not, then there is 1 digit, if yes, then divide by 100 and check if the number is greater than 1 - if not, then that there are 2 digits, if so, divide by 1000, etc.
    This is the dumbest yet simplest method I can think of ;-)
    You can also convert a number to a string and check the number of characters in the string...
    As you can see, there are many methods...
  • ADVERTISEMENT
  • #3 741522
    BoskiDialer
    Level 34  
      unsigned long int o;
      // przypisanie wartosci która ulegnie przebadaniu
      o = 123456
    
      int i;
      for(i=0;o>0;i++)
        o = o / 10;
      if (i==0) i=1;
    
      // odczyt ilosci tetrad podanej liczby
      jakas_zmienna = i;
ADVERTISEMENT