Fibonacci Series In C Using For Loop
In this tutorial, we are going to see how to write a C program to display Fibonacci series using For loop.
There are two ways to display Fibonacci series of a given number, by using for loop, or by recursive function.
Fibonacci series is a sequence of integers of 0, 1, 2, 3, 5, 8…
The first two numbers are 0 and 1. All the other numbers are obtained by adding the two previous numbers. This means that the nth number is the sum of the (n-1)th and (n-2)th number.
Fibonacci Series In C Using For Loop
#include <stdio.h> int main() { int n, nbr1 = 0, nbr2 = 1, next, i; printf("Enter the number of terms: "); scanf("%d", &n); printf("The first %d terms of the Fibonacci series are: \n", n); for (i = 0; i < n; i++) { if (i <= 1) next = i; else { next = nbr1 + nbr2; nbr1 = nbr2; nbr2 = next; } printf("%d\n", next); } return 0; }
This example will print the Fibonacci sequence of 8.
Enter the number of terms: 8 The first 8 terms of the Fibonacci series are: 0 1 1 2 3 5 8 13