HackerRank C- Calculate the Nth term

There is a series, S, where the next term is the sum of pervious 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.

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. //Complete the following function.
  6. int find_nth_term (int n, int a, int b, int c) {
  7. if(n == 1) return a;
  8. if(n == 2) return b;
  9. if(n == 3) return c;
  10. 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);
  11. }
  12. int main() {
  13. int n, a, b, c;
  14. scanf("%d %d %d %d", &n, &a, &b, &c);
  15. int ans = find_nth_term(n, a, b, c);
  16. printf("%d", ans);
  17. return 0;
  18. }


Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube