In this HackerRank Calculate the Nth term problem in the C programming problem there is a series where the next term is the sum of the previous three terms. Given the first three terms of the series a,b, and c respectively, you have to output the nth term of the series using recursion.


HackerRank Calculate the Nth term in c problem solution

HackerRank Calculate the Nth term in c problem solution

 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int find_nth_term (int n, int a, int b, int c) {
    if(n == 1) return a;
    if(n == 2) return b;
    if(n == 3) return c;
    return find_nth_term(n-1, a, b, c) + find_nth_term(n-2, a, b, c) + find_nth_term(n-3, a, b, c);
}