[51432 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.

- Algorithm and Flowchart to Print the Reverse of the Number
- Algorithm and Flowchart for N Queen Problem with Simple Explanation
- Prefix to Infix notation Algorithm and Flowchart
- Algorithm and Flowchart to find the inverse of an array
- Algorithm and Flowchart to Count no. of Vowels, Consonants and Special Character in a string

- Algorithm and Flowchart to find if a Number is Even or Odd
- Algorithm and Flowchart to find whether a number is Prime Number or Not
- Algorithm and Flowchart for Armstrong Number
- Algorithm for Finding Factorial of a Number
- Algorithm and Flowchart to Calculate Fibonacci series up to n
- How to round a number to nearest integer using python
- Can we have DB table without a primary key?
- Why does everyone hate Java?
- How much JavaScript to learn for Web Development
- How to store password like sensitive information in application