Calculate the Nth term

Sort by

recency

|

817 Discussions

|

  • + 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) {
      //Write your code here.
      if (n == 1) return a;
      if (n == 2) return b;
      if (n == 3) return c;
      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);
    }
    
    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;
    }
    
  • + 0 comments

    include

    int find_nth_term(int n, int a, int b, int c) { int sum[n],count; sum[0]=a,sum[1]=b,sum[2]=c; for(int i=3;i

  • + 1 comment
    #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 term, t1 = a, t2 = b, t3 = c;
        if (n == 1)
            term = t1;
        else if (n == 2)
            term = t2;
        else if (n == 3)
            term = t3;
        else {
            for (int i = 4; i <= n; i++) {
                term = t1 + t2 + t3;
                t1 = t2;
                t2 = t3;
                t3 = term;
            }
        }
        return term;
    }
    
    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;
    }
    
  • + 0 comments

    include

    include

    include

    include

    //Complete the following function.

    int find_nth_term(int n, int a, int b, int c) { int term, t1 = a, t2 = b, t3 = c; if (n == 1) term = t1; else if (n == 2) term = t2; else if (n == 3) term = t3; else { for (int i = 4; i <= n; i++) { term = t1 + t2 + t3; t1 = t2; t2 = t3; t3 = term; } } return term; }

    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;
    

    }

  • + 0 comments

    include

    include

    include

    include

    //Complete the following function.

    int find_nth_term(int n, int a, int b, int c) { int term, t1 = a, t2 = b, t3 = c; if (n == 1) term = t1; else if (n == 2) term = t2; else if (n == 3) term = t3; else { for (int i = 4; i <= n; i++) { term = t1 + t2 + t3; t1 = t2; t2 = t3; t3 = term; } } return term; }

    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;
    

    }