Calculate Product of Digits using Recursion in C

Spread the love

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:


Enter a Number : 1245


Product of digits of number 1245 is : 40


It is recommended to have basic knowledge of functions and recursion in C language. Please check out the following articles.

Product of Digits using Recursion in C Program Algorithm/Explanation:

  1. Start the program by taking the user input. Store the input value in a variable called num.
  2. Check for negative numbers using the if(num <= 0) condition. Use Goto Statement to ask for the input again.
  3. 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.
  4. Call the prodOfDigits function from the main function and pass the input number num. Store the return value in product variable.
  5. Display the results on the console.

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:

Related Functions Practice Programs:


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

You may also like...

1 Response

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

Leave a Reply