# Finding Nth term of a fibonacci series in C

## Nth Term of Fibonacci Series

Today in this article we will learn about how to find Nth term of Fibonacci series in C language.

The Fibonacci sequence is a series of numbers in which each number is the sum of the two that precede it. Starting at 0 and 1 the sequence looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on forever

Lets understand this with the help of example :-

• Input:- 3
• Output:- 1

Here we need to find the 3rd term of  the Fibonacci series that is 1.

## Algorithm

• First we will take two variables t1 and t2 and nextTerm to store the addition of previous two elements.
• Take the value of n that term that we need to find in the Fibonacci series.
• If the value of n is equal to 0 or 1 then print n itself.
• Else store the sum of t1 and t2 in the nextTerm variable.
• To add the last to element iterate from i=3 to n and store t2 in t1 -> t1 = t2;  and initialize t2 with nextTerm  t2 = nextTerm; then  nextTerm = t1 + t2 (sum of t1 and t2)
• Out of the loop print the value that is t2

## C Code (Method 1) :-

```#include<stdio.h>

int main() {

int n, t1 = 0, t2 = 1, nextTerm = 0, i;
printf("Enter the nth value: ");
scanf("%d", &n);
if (n == 0 || n == 1)
printf("%d", n);
else
nextTerm = t1 + t2;

for (i = 3; i <= n; ++i) {
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("%d", t2);
}```

## Output:-

```Enter the nth value: 5
3```

## Method 2 (Recursion)

The recursive function to find the nth Fibonacci term is based on below three conditions.

• If num == 0 then return 0. As the Fibonacci of 0th term is 0.
• If num == 1 then return 1. As The Fibonacci of 1st term is 1.
• If num > 1 then return fibo(num – 1) + fibo(n-2). As the Fibonacci of a term is sum of previous two terms.
```#include<stdio.h>

int fibo(int num);
int main() {

int num;
int fibonacci;
// Input from the user
printf("Enter any number to find nth fiboacci term: ");
scanf("%d", &num);

fibonacci = fibo(num);
printf("%d fibonacci term is %d", num, fibonacci);

return 0;
}
// Recursive function to find nth Fibonacci term
int fibo(int num) {

if (num == 0)  // Base condition
return 0;
else if (num == 1)  // Base condition
return 1;
else
return fibo(num - 1) + fibo(num - 2);
}```

## Output:-

```Enter the nth value: 5
3```