In this tutorial, i am going to show you how to count or find number of digits in a number in c program with the help of For Loop, While Loop, Functions, and Recursion.

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

Use the following steps to write a program to count number of digits in a number; as follows:

- Read integer number and store it into a variable.
- Initialize another variable to store total digits say count = 0.
- If num > 0 then increment count by 1 i.e. count++.
- Divide num by 10 to remove the last digit of the given number i.e. num = num / 10.
- Repeat step 3 to 4 till num > 0 or num != 0.
- Print number of count in a number

## All C Programs to Count Number of Digits in a Number

- C Program to Count Number of Digits in a Number using While Loop
- C Program to Count Number of Digits in a Number using For Loop
- C Program to Count Number of Digits in a Number Using Functions
- C Program to Count Number of Digits in a Number Using Recursion

### C Program to Count Number of Digits in a Number using While Loop

/* C Program to Count Number of Digits in a Number using While Loop */ #include <stdio.h> int main() { int Number, Reminder, Count=0; printf("\n Please Enter any number :- "); scanf("%d", &Number); while(Number > 0) { Number = Number / 10; Count = Count + 1; } printf("\n Number of Digits in a Given Number = %d", Count); return 0; }

The result of the above c program; as follows:

Please Enter any number :- 123456 Number of Digits in a Given Number = 6

### C Program to Count Number of Digits in a Number using For Loop

/* C Program to Count Number of Digits in a Number using For Loop */ #include <stdio.h> int main() { int Number, Reminder, Count; printf("\n Please Enter any number :- "); scanf("%d", &Number); for (Count=0; Number > 0;Number=Number/10) { Count=Count + 1; } printf("\n Number of Digits in a Given Number = %d", Count); return 0; }

The result of the above c program; as follows:

Please Enter any number :- 145698 Number of Digits in a Given Number = 6

### C Program to Count Number of Digits in a Number Using Functions

/* C Program to Count Number of Digits in a Number using While Loop */ #include <stdio.h> int Count_Of_Digits (int Number) { int c = 0; while(Number > 0) { Number = Number / 10; c = c + 1; } printf("\n Number of Digits in a Given Number = %d", c); } int main() { int Number; printf("\n Please Enter any number :- "); scanf("%d", &Number); Count_Of_Digits (Number); return 0; }

The result of the above c program; as follows:

Please Enter any number :- 123456789 Number of Digits in a Given Number = 9

### C Program to Count Number of Digits in a Number Using Recursion

/* C Program to Count Number of Digits in a Number Using Recursions */ #include <stdio.h> int Count_Of_Digits (int); int main() { int Number, Count = 0; printf("\n Please Enter any number :- "); scanf("%d", &Number); Count = Count_Of_Digits (Number); printf("\n Number of Digits in a Given Number = %d", Count); return 0; } int Count_Of_Digits (int Number) { static int Count =0; if(Number > 0) { Count = Count + 1; Count_Of_Digits (Number / 10); } return Count; }

The result of the above c program; as follows:

Please Enter any number :- 41236 Number of Digits in a Given Number = 5