Calculate the Nth term

  • + 0 comments
    #include <stdio.h>
    
    // Recursive function to calculate the nth term
    int find_nth_term(int n, int a, int b, int c) {
        // Base case: if n is 1, return a
        if (n == 1) {
            return a;
        }
        // Base case: if n is 2, return b
        else if (n == 2) {
            return b;
        }
        // Base case: if n is 3, return c
        else if (n == 3) {
            return c;
        }
        // Recursive case: calculate the nth term as the sum of the previous three terms
        else {
            return find_nth_term(n - 1, b, c, a + b + c);
        }
    }
    
    int main() {
        int n, a, b, c;
    
        // Read the input values
        scanf("%d", &n);
        scanf("%d %d %d", &a, &b, &c);
    
        // Calculate and print the nth term
        int result = find_nth_term(n, a, b, c);
        printf("%d", result);
    
        return 0;
    }