Calculate Product of Digits using Recursion in C

Product of Digits using Recursion in C Program Description:
Write a Program to find the Product of Digits of a number using Recursion in C programming language, The program should take an integer number as input and calculate the product of its digits.
Excepted Input and Output:
Input:
Enter a Number : 1245
Output:
Product of digits of number 1245 is : 40
Pre-Requisites:
It is recommended to have basic knowledge of functions and recursion in C language. Please check out the following articles.
- How to Define, Declare and call functions in C Langauge with Example Programs
- How Recursion works in Programming with Step by step explanation
Product of Digits using Recursion in C Program Algorithm/Explanation:
- Start the program by taking the user input. Store the input value in a variable called num.
- Check for negative numbers using the if(num <= 0) condition. Use Goto Statement to ask for the input again.
- Create a function called
prodOfDigits. The
prodOfDigits function takes an integer variable as input and calculates the product of digits of the given number using recursion and returns the result back to the caller.
- The prodOfDigits uses recursion to calculate the product of each digit. So we need to have a base condition for recursion. which is num == 0. If we reach the base condition, return 1.
- If the
num is not equal to zero.
- Get the last digit of the num using Modulus Operator ( num % 10)
- Make a recursive call to prodOfDigits(num/10) function and get the return value.
- Multiply the above two results. – ( (num % 10) * prodOfDigits(num / 10) )
- Note that we are using num/10 in a recursive call to prodOfDigits function, The division operation ( num/10) removes the last digit from the num, As we already used the last digit in the calculation of the product.
- The above recursive calls continue till we reach the base condition.
- Call the prodOfDigits function from the main function and pass the input number num. Store the return value in product variable.
- Display the results on the console.
Related Program: Product of Digits of a Number using Iterative Method
Product of Digits using Recursion in C Program:
If you are testing with large numbers, Please use the
long and
long long int datatype for the
num, prod variables. so that the product won’t reach integer limits.
Program Output:
Compile the program
$ gcc product-of-digits-recursion.c
Run the Program
Test Case 1: Positive Numbers
Test Case 2: Numbers with Zeros

As we can see from the above output, We are getting the desired results.
Test Case 3: Negative Numbers
The user will be prompted by the application until a correct input number is given.
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 Product the Number of digits in a Number using Recursion […]
[…] C Program to Product the Number of digits in a Number using Recursion […]