We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C
- Functions
- Calculate the Nth term
- Discussions
Calculate the Nth term
Calculate the Nth term
Sort by
recency
|
792 Discussions
|
Please Login in order to post a comment
I think it is better way !
include
include
include
//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; else if(n==2) return b; else if(n==3) return c; else{ int sum=0; for(int i=1;i<=n-3;i++){ sum=a+b+c; a=b; b=c; c=sum; } return sum; } }
int main() { int n, a, b, c;
}`
include
include
include
include
//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; else if(n==2) return b; else if(n==3) return c; else{ int sum=0; for(int i=1;i<=n-3;i++){ sum=a+b+c; a=b; b=c; c=sum; } return sum; } }
int main() { int n, a, b, c;
}
include
int fibo(int n,int a[]) { if(n == 1) { return a[0]; } if(n == 2) { return a[1] ; } if(n == 3) { return a[2]; } return fibo(n - 1,a) + fibo(n - 2,a) + fibo(n - 3,a); } int main () { int n; scanf("%d", &n); int a[3]; scanf("%d%d%d", &a[0], &a[1], &a[2]); int c = fibo(n,a); printf("%d",c); return 0;
}**
include
int fibo(int n,int a[]) { if(n == 1) { return a[0]; } if(n == 2) { return a[1] ; } if(n == 3) { return a[2]; } return fibo(n - 1,a) + fibo(n - 2,a) + fibo(n - 3,a); } int main () { int n; scanf("%d", &n); int a[3]; scanf("%d%d%d", &a[0], &a[1], &a[2]); int c = fibo(n,a); printf("%d",c); return 0; }