Calculate the Nth term

Sort by

recency

|

810 Discussions

|

  • + 0 comments

    Coding becomes easy with cookout hamburger!

  • + 0 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;

    // 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;
    

    }

  • + 0 comments

    include

    int main() { int n, a, b, c;

    scanf("%d", &n);
    scanf("%d %d %d", &a, &b, &c);
    
    int last_three[3];
    last_three[0] = a;
    last_three[1] = b;
    last_three[2] = c;
    
    int sum = last_three[0] + last_three[1] + last_three[2];
    
    if (n==3) {
        printf("%d", c);
    }
    
    else if (n==2){
        printf("%d", b);
    }
    
    else if (n==1){
        printf("%d", a);
    }
    
    else {
    
    for (int i = 3; i < n-1; i++) {
        last_three[0] = last_three[1];
        last_three[1] = last_three[2];
        last_three[2] = sum;
    
        sum = last_three[0] + last_three[1] + last_three[2];
    }
    
    printf("%d\n", sum);
    }
    
    return 0;
    

    }

  • + 0 comments
    #include<stdio.h>
    
    int main() {
        int n, a, b, c;
    
        scanf("%d", &n);
        scanf("%d %d %d", &a, &b, &c);
    
        int last_three[3];
        last_three[0] = a;
        last_three[1] = b;
        last_three[2] = c;
    
        int sum = last_three[0] + last_three[1] + last_three[2];
    
        if (n==3) {
            printf("%d", c);
        }
    
        else if (n==2){
            printf("%d", b);
        }
    
        else if (n==1){
            printf("%d", a);
        }
    
        else {
            
        for (int i = 3; i < n-1; i++) {
            last_three[0] = last_three[1];
            last_three[1] = last_three[2];
            last_three[2] = sum;
    
            sum = last_three[0] + last_three[1] + last_three[2];
        }
    
        printf("%d\n", sum);
        }
    
        return 0;
    }
    
  • + 1 comment
    int find_nth_term(int n, int a, int b, int c) {
      if(n==1){
          return a;
      }
      else if(n==2){
          return b;
      }
      else if (n==3) {
          return c;
      }
      else{
          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);
      }
    }