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.
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;
scanf("%d %d %d %d", &n, &a, &b, &c);
int ans = find_nth_term(n, a, b, c);
printf("%d", ans);
return 0;
}`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Calculate the Nth term
You are viewing a single comment's thread. Return to all comments →
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;
}`