Calculate the Nth term

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    //Complete the following function.
    
    int find_nth_term(int n, int a, int b, int c) {
        int sum = 0;
        if (n==1){
            sum = a;
        }
        else if (n==2){
            sum = b;
        }
        else if (n==3){
            sum = c;
        }
        else if (n>3){
            for (int i=n-3; i<n; i++){
                sum += find_nth_term(i, a, b, c);
            }
        }
        return sum;
    }
    
    int main() {
        int n, a, b, c;
      
        scanf("%d %d %d %d", &n, &a, &b, &c);
        int ans = find_nth_term(n, a, b, c);
     
        printf("%d", ans); 
        return 0;
    }