# Product of N Numbers using Recursion in C Language

## Product of N Numbers using Recursion in C Program Description:

Write a Program to calculate the Product of N Numbers using Recursion in C programming language. The program should accept a positive number and calculate the product of first n natural numbers using the recursive.

### Example Input and Output:

Input:

Enter a Number (n): 10

Output:

Product of first 10 numbers is : 3628800

### Pre-Requisites:

It is recommended to know the basics of the Functions in C Language, Recursion in Programming, and Relational Operators.

## Product of N Numbers using Recursion in C Explanation:

1. Take the input from the user and store it in a variable named n.
2. Check if the number n is a negative number, If so display the error message ERROR! Enter a Valid Number. And ask for the user input again.
• We use the Goto Statement to jump back to the program start.
• use goto INPUT; and Label INPUT:
• The user will be prompted for the input until a valid number is provided.
3. Create a Function named productNNumbers. The productNNumbers takes an integer n as input and calculates the product of the first n numbers using the recursion and returns the result back to the caller. Here are the details of the ProductNNumber function.
• As we are using recursion, We need to define the base condition to stop the recursive calls. Here function base condition is n == 1. So if the n reaches the 1, return 1
• Otherwise (if n > 1), Then make a recursive call to the productNNumbers with n-1. Multiply the result with n. – n * productNNumbers(n-1)
• The above recursive calls continue until the n value reaches 1, That is when the base condition will be evaluated as true and recursive functions will return one by one.
4. Call the productNNumbers function from the main function. Pass the given number n to productNNumbers function. Store the return value in result variable. ( result = productNNumbers(n);)
5. Finally, Display the results on the console.

## Product of N Numbers using Recursion in C Program:

Here is the Program of Product of N Natural Numbers in C language using the recursive method.

The prototype details of the productNNumbers function

• Function_name: productNNumbers
• Argument_List: int
• Return_value: int

### Program Output:

Compile the program

gcc product-n-numbers.c

Run the Program.

Test Case 1: Positive Numbers

Test Case 2: Negative Numbers:

We are getting the excepted results. Program is displaying the error message if the user enters a negative number and asking for the user input again.

📢 If you need to calculate the product for large numbers, Then use the long int and long long int for result and n variables.

Related: Limits and Sizes of Datatypes in C

## Related 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.

### 4 Responses

1. […] Product of N natural Number using Recursion C Program […]

2. […] C Program to calculate the Product of N Natural Numbers using Recursion […]

3. […] C Program to calculate the Product of N Natural Numbers using Recursion […]

4. […] C Program to calculate the Product of N Natural Numbers using Recursion […]