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 n, int a, int b, int c){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 = 5, a = 1, b = 2, c = 3;int ans = find_nth_term(n, a, b, c);printf("%d", ans);return 0;}
No comments:
Post a Comment