[79881 views]

In this program we will be counting the no. of digits in the input integer. For Eg. lets say we input 342567
so the no. of digits is 6.

There are two different method for counting the digits.

- Using a loop whether it be a while or recursive loop.
- Using a log but that is applicable only for positive numbers.

Let's consider we have a number 342567 so how can we count its digit? First thing is clear that we have to use a loop as we are counting the digits, as long as the loop will run we will add the 1 to the variable. But we don't know the length of integer so what we can do is divide it by 10.We divide until the number becomes zero so loop will run until the length of digits. Though this we can count no. of digits in a given integer.

Here in this algorithm we declare a variable "a" for storing the number whose digits we have to count and a variable n for counting the digits. We read the variable a. Let's take a number 456235 we have to count it's digit, so we store it in the variable a. As our number is not zero it will enter the loop, this will be the 1st iteration.

- In the 1st iteration, comes a=a/10 so we divide 456235/10 ie. a becomes 45623 and we add a number 1 in n, n becomes 1.
- In the 2nd iteration 45623 enters the loop a becomes 4562 and n becomes 2
- In the 3rd iteration 4562 enters the loop a becomes 456 and n becomes 3.
- In the 4th iteration 456 enters the loop a becomes 45 and n becomes 4.
- In the 5th iteration 45 enters the loop a becomes 4 n becomes 5.
- In the 6th iteration a becomes 0 and n becomes 6

**Note:** The value stored in the variable is int hence the digit should be in between -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
depending on whether you have 32 bit or 64 bit compiler. At max a variable can store 9223372036854775807 this much large number using
long datatype.

- Java IDE Online
- Python IDE Online
- JSON Formatter/Minifier Online
- Case Converter
- Reverse String
- HTML Encoder
- HTML Decoder
- URL Encoder
- URL Decoder
- Decimal To Binary
- Binary To Decimal
- Decimal To Octal
- Binary To Octal
- Decimal To Hexadecimal
- Hexadecimal To Decimal
- Hexadecimal To Binary
- Octal To Decimal
- Octal to Hexadecimal
- Octal to Binary
- Calculate String Length
- Remove Spaces
- Remove Line Breaks
- Remove Empty Lines
- Remove Duplicate Lines
- Word Counter
- Replace Space with hyphen
- Check Armstrong number
- Text to URL

- Algorithm and Flowchart to convert temperature from Celsius to Fahrenheit
- Algorithm and Flowchart to check whether a given number is magic number or not
- Algorithm and Flowchart to find the smallest element in an array
- Algorithm and Flowchart to Find Area and Circumference of Circle
- Algorithm to Count number of Words in Sentence for Beginners