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