Calculate the Nth term

Write a program to find the nth term of the series.
S(n)={ a if(n==1),b if(n==2), c if(n==3), otherwise s(n-1)+s(n-2)+s(n-3))
Example:
Input:  n = 5, a = 1, b=2,c=3
Output: 11

C Program

#include <stdio.h>
int find_nth_term(int nint aint bint c)
{
    if (n == 1)
        return a;
    if (n == 2)
        return b;
    if (n == 3)
        return c;
    return find_nth_term(n - 1abc) + find_nth_term(n - 2abc) + find_nth_term(n - 3abc);
}

int main()
{
    int n = 5a = 1b = 2c = 3;

    int ans = find_nth_term(nabc);

    printf("%d"ans);
    return 0;
}


No comments:

Post a Comment