The one direction is always partying in some room or the other. To gair
entry into the room, one needs to know the fibonacci sequence.
The first ten terms of the fibonacci sequence are:
0,1, 1, 2, 3, 5, 8, 18, 21, 34, ... and so on. (Each term is the sum of
previous two terms)
You have to tell the nth term of the sequence.
Input:
A single integer n.
Output:
The n'th term in the fibonacci sequence. Print a \n at the end of the
answer.
Constraints:
1<=n<=15
SAMPLE INPUT
Attachments:
Answers
Answered by
0
Answer:
int main() {
int i=1, n, t1 = 0 , t2 = 1 , nextTerm;
scanf("%d", &n);
if(n==1 || n==0){
printf("%d", n);
}else {
nextTerm = t1 + t2;
}
for ( i=3; i<=n; ++i){
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("%d", t2);
return 0;
}
Explanation:
Its pretty simple just take a look into the code
Similar questions