# Program to Count number of digits using Recursion in C Language

## Count number of Digits using recursion in C Program Description:

Write a Program to count number of digits using recursion in C Programming language. The program should take an integer number as input and calculate number of digits using recursion.

### Example Input and Output:

Input:

Enter a number to check no of digits : 3243241

Output:

Number of Digits in 3243241 are : 7

## Count Number of Digits using Recursion in C Program Explanation:

1. Take the input number from the user and store it in the variable named num.
2. Check if the num is a positive number. If the num is a negative number, then display an error message and prompt the user to input the number again using the Goto Statement (The goto statement is used to jump from one statement in function to another statement, Use the goto START; statement and START: label )
3. Define a function called countDigits, The countDigits function takes num as the input and calculates the number of digits in num using recursion. The countDigits function makes recursive calls to itself to calculate the number of digits. Finally, it returns the results back to the caller.
• The base condition for recursion is num == 0, If the number reaches zero, Stop the recursion and return zero(0).
• If the num is not zero, Make the recursive call to countDigits function. Pass the num/10 as the parameter. The num/10 removes the last digit from the number. – i.e – return (1 + countDigits(num/10));
4. Call the countDigits function from the main() function. Provide the input number num as input and store the return value in nDigits variable.
5. Display the results on the console.

## Count Number of Digits using Recursion in C Program:

Conver the above logic into the C Program.

Prototype details of the countDigits function.

• int countDigits(int);
• Function Name: countDigits
• Arguments_list: int
• Return_type: int

### Program Output:

Compile and run the program using GCC compiler ( any compiler)

Test Case 1: Positive Numbers:

Test Case 2: Numbers with Zeros:

As we can see from the above output, We are getting the expected results.

Test Case 3: Negative Numbers:

If the user enters a negative number, The program will display an error message and asks for the input again.

## Related Functions Practice Programs: Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 2 Responses

1. […] C Program to Count the Number of digits in a Number using Recursion […]

2. […] C Program to Count the Number of digits in a Number using Recursion […]