## Q. Write a algorithm and program to count the number of digits in a number.

Given a number and the task is to write a algorithm and program a program to count the number of digits in a number. A number is a mathematical object which is used to count or measurement. So we are given a input number and we need to print the count of that number.

For Example :- We have given input number as 12345 and the count of the number is 5.So, output is 5.
Input :- 12345
Output :- 5

Explanation:
1. First declare a integer variable (num & count) and initialize an empty integer num to "1234" and count to 0.

2. Now we have to calculate the count of the digits in number which we can do using while loop( test expression num != 0 ).
A. After 1st interation the value of num will be 1234 and the count is incremented to 1.
B. After 2nd interation the value of num will be 123 and the count is incremented to 2.
C. After 3rd interation the value of num will be 12 and the count is incremented to 3.
D. After 4th interation the value of num will be 1 and the count is incremented to 4.
E. In the start of 5th interation, the value of num will be 0 and the loop will be terminated.

3. After the loop terminated, print the value stored in count.

#### Algorithm to count the number of digits in a given number

```START
step 1 : Input a number from user. Store it in some variable say num.
step 2 : Initialize another variable to store total digits say digit = 0.
step 3 : If num > 0 then increment count by 1 i.e. count++.
step 4 : Divide num by 10 to remove last digit of the given number i.e. num = num / 10.
step 5 : Repeat step 3 to 4 till num > 0 or num != 0.
STOP
```

#### Program to count the number of digits in a number

• C
• C++
• Java
• Python
• C#
• PHP
• ```#include <stdio.h>
int count_digit(long long num)
{
int count = 0;
while (num != 0) {
num = num / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long num = 874512369;
printf("Number of digits : %d",
count_digit(num));
return 0;
}
```
```#include <iostream>
using namespace std;
int count_digit(long long num)
{
int count = 0;
while (num != 0) {
num = num / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long num = 874512369;
cout << "Number of digits : "
<< count_digit(num);
return 0;
}
```
```class LFC {
static int count_digit(long num)
{
int count = 0;
while (num != 0) {
num = num / 10;
++count;
}
return count;
}
/* Driver program to test above function */
public static void main(String[] args)
{
long num = 874512369;
System.out.print("Number of digits : " + count_digit(num));
}
}
```
```def count_digit(num):
count = 0
while num != 0:
num //= 10
count+= 1
return count
# Driver Code
num = 874512369
print ("Number of digits : % d"%(count_digit(num)))
```
```using System;
class LFC {
static int count_digit(long num)
{
int count = 0;
while (num != 0) {
num = num / 10;
++count;
}
return count;
}
/* Driver program to test
above function */
public static void Main()
{
long num = 874512369;
Console.WriteLine("Number of"
+ " digits : " + count_digit(num));
}
}
```
```function count_digit(\$num)
{
\$count = 0;
while (\$num != 0)
{
\$num = round(\$num / 10);
++\$count;
}
return \$count;
}
// Driver code
\$num = 874512369;
echo "Number of digits : "
. count_digit(\$num);
```
```#include <stdio.h>
int count_digit(long long num)
{
int count = 0;
while (num != 0) {
num = num / 10;
++count;
}
return count;
}
// Driver code
int main(void)
{
long long num = 874512369;
printf("Number of digits : %d",
count_digit(num));
return 0;
}
```

#### Output

```Number of digits : 9
```

## Recommended Programs

Program to find factorial of a number
Program to check strings are anagram or not