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
Pre-Requisites:
Please go through the following articles to learn more about the Functions and recursion in C language.
Count Number of Digits using Recursion in C Program Explanation:
- Take the input number from the user and store it in the variable named num.
- 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 )
- 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));
- Call the countDigits function from the main() function. Provide the input number num as input and store the return value in nDigits variable.
- Display the results on the console.
Count Number of Digits using Recursion in C Program:
Conver the above logic into the C Program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* Program to calculate the number of digits in given number using recursion in c */ #include <stdio.h> /** * @brief Count number of digits in given number 'num' * * @param num - number * @return int - number of digits */ int countDigits(int num) { // base condition if(num == 0) { return 0; } // make recursive call to 'countDigits' function return (1 + countDigits(num/10)); } int main() { // User Input START: // goto statement int num; printf("Enter a number to check no of digits : "); scanf("%d", &num); // Negative numbers check if(num <= 0) { printf("Error: Please enter a positive number, Try again\n"); goto START; } // call the 'countDigits' function int numDigits = countDigits(num); // Print the results printf("Number of Digits in %d are : %d \n", num, numDigits); return 0; } |
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:
1 2 3 4 5 6 7 8 9 10 11 |
$ gcc n-Digits-recursion.c $ ./a.out Enter a number to check no of digits : 1234 Number of Digits in 1234 are : 4 $ ./a.out Enter a number to check no of digits : 3243241 Number of Digits in 3243241 are : 7 $ ./a.out Enter a number to check no of digits : 94324 Number of Digits in 94324 are : 5 $ |
Test Case 2: Numbers with Zeros:
1 2 3 4 |
$ ./a.out Enter a number to check no of digits : 1000 Number of Digits in 1000 are : 4 $ |
As we can see from the above output, We are getting the expected results.
Test Case 3: Negative Numbers:
1 2 3 4 5 6 7 8 |
$ ./a.out Enter a number to check no of digits : -12445 Error: Please enter a positive number, Try again Enter a number to check no of digits : -124 Error: Please enter a positive number, Try again Enter a number to check no of digits : 64 Number of Digits in 64 are : 2 $ |
If the user enters a negative number, The program will display an error message and asks for the input again.
More Recursion Practice Programs:
- C Program to Find Factorial of Number using Recursion
- C Program to Generate Fibonacci Number using Recursion.
- C Program to calculate the sum of Digits of Number using Recursion
- C Program to Print First N Natural Numbers using Recursion
- C Program to find sum of Fist N Natural Number using Recursion
- Product of N natural Number using Recursion C Program
Related Functions Practice Programs:
- Arithmetic Operations using functions
- Fibonacci Series using function
- Factorial of a Number using functions
- Armstrong Number using Function in C language
- Prime number program using Function
- Program to Caclulate Area of Circle using Function
- Program to Check Even or Odd number using Function
- Binary to Decimal Conversion using function in C
- 100+ C Practice Programs
- C Tutorials Index
2 Responses
[…] C Program to Count the Number of digits in a Number using Recursion […]
[…] C Program to Count the Number of digits in a Number using Recursion […]