We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Recursive function to find the nth term of the series
int recurse(int n, int a, int b, int c) {
// Base cases
if (n == 1) return a;
if (n == 2) return b;
if (n == 3) return c;
// Recursive case
return recurse(n-1, a, b, c) + recurse(n-2, a, b, c) + recurse(n-3, a, b, c);
}
int main() {
int n, a, b, c;
// Input n and the first three terms of the series
scanf("%d", &n);
scanf("%d %d %d", &a, &b, &c);
// Output the nth term of the series
printf("%d\n", recurse(n, a, b, c));
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Calculate the Nth term
You are viewing a single comment's thread. Return to all comments →
//Copy all this code and paste it
include
// Recursive function to find the nth term of the series int recurse(int n, int a, int b, int c) { // Base cases if (n == 1) return a; if (n == 2) return b; if (n == 3) return c;
}
int main() { int n, a, b, c;
}