# Solving f(n)= (1) + (2*3) + (4*5*6) … n using Recursion

Example:

```Input : 2
Output: 7
Series: (1) + (2*3)

Input : 4
Output: 5167
Series: (1) + (2*3) + (4*5*6) + (7*8*9*10) ```

## C

```// Program to print the solution of the series
// f(n)= (1) + (2*3) + (4*5*6) ... n using recursion
#include <stdio.h>

// Recursive function for finding sum of series
// calculated - number of terms till which sum of terms has
//              been calculated
// current    - number of terms for which sum has to be
//              calculated
// N          - Number of terms in the function to be calculated
int seriesSum(int calculated, int current, int N)
{
int i, cur = 1;

// checking termination condition
if (current == N + 1)
return 0;

// product of terms till current
for (i = calculated; i < calculated + current; i++)
cur *= i;

// recursive call for adding terms next in the series
return cur + seriesSum(i, current + 1, N);
}

int main()
{
// input number of terms in the series
int N = 5;

// invoking the function to calculate the sum
printf("%d\n", seriesSum(1, 1, N));

return 0;
}
```

## Java

```/* Java Program to print the solution of the series
f(n)= (1) + (2*3) + (4*5*6) ... n using recursion */

class Test
{

/**
* Recursive method for finding sum of series
*
* @param calculated number of terms till which sum of terms has been calculated
* @param current number of terms for which sum has to be calculated.
* @param N Number of terms in the function to be calculated
* @return sum
*/

static int seriesSum(int calculated, int current, int N)
{
int i, cur = 1;

// checking termination condition
if (current == N + 1)
return 0;

// product of terms till current
for (i = calculated; i < calculated + current; i++)
cur *= i;

// recursive call for adding terms next in the series
return cur + seriesSum(i, current + 1, N);
}

// Driver method
public static void main(String[] args)
{
// input number of terms in the series
int N = 5;

// invoking the method to calculate the sum
System.out.println(seriesSum(1, 1, N));
}
}
```

Output:
`365527 `

Source : http://qa.geeksforgeeks.org/6597/wap-a-program-for-f-n-1-2-3-4-5-6-n-using-recursion/

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2 Average Difficulty : 2/5.0
Based on 3 vote(s)